用于2d char数组的C和C ++分配内存。 [英] C and C++ allocation memory for 2d char array.

查看:66
本文介绍了用于2d char数组的C和C ++分配内存。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态创建2D char数组的内存,相同的代码在C中工作正常,但它不能在C ++中工作。



我有两个问题:

1.为什么C代码工作正常& C ++不是用于2D字符串数组动态内存分配吗?

2.如果C ++代码错误或我错过了C ++代码中的任何内容;请纠正我。告诉我动态分配2D char数组的实际实现。



请看下面的两个(C / C ++)代码。



C代码:

I am trying to create memory for 2D char array dynamically, the same code is working fine in C but its not working in C++.

I have two question:
1. Why C code is working fine & C++ not for 2D char array dynamically memory allocation?
2. If C++ code is wrong or I missed out anything in the C++ code; please correct me. Tell me the actual implementation to allocate 2D char array dynamically.

Please look the below both (C/C++)code.

C code:

#include<stdio.h>
#include<stdlib.h>
void main()
{
    char **array;
    int row,column;
    char temp='A';
    printf("enter the row");
    scanf("%d",&row);
    printf("enter the column");
    scanf("%d",&column);
    array=(char **)malloc(row*sizeof(char *));
    for (int i=0;i<row;i++)
    {
        array[i]=(char*)malloc(column*sizeof(char));
    }

    for(int j=0;j<row;j++)
    {
        for (int k=0;k<column;k++)
        {
            printf("%d\n",&array[j][k]);

            array[j][k]=temp;
            printf("%c\n",*(*(array+j)+k));

            temp++;
        }
    }
}



C ++代码:


C++ code:

#include<iostream.h>
#include<stdlib.h>
void main()
{
    char **array;
    int row,column;
    char temp='A';
    cout<<"enter the Row"<<endl;
    cin>>row;
    cout<<"enter the Column"<<endl;
    cin>>column;
    array=(char **)malloc(row*sizeof(char *));
    for (int i=0;i<row;i++)>
    {
        array[i]=(char*)malloc(column*sizeof(char));
    }

    for(int j=0;j<row;j++)>
    {
        for (int k=0;k<column;k++)>
        {
            cout<<&array[j][k]<<endl;
            array[j][k]=temp;
            cout<<*(*(array+j)+k)<<endl;
            temp++;
        }
    }
}



[edit]已添加代码块 - OriginalGriff [/ edit]


[edit]Code block added - OriginalGriff[/edit]

推荐答案

首先,你的C ++实现应该使用

First of, your C++ implementation should use
#include <iostream> // without the .h!





那么,我认为你遇到的麻烦是造成的by the line



Then, I assume the trouble you have is caused by the line

cout<<&array[j][k]<<endl;



格式化流i / o的好处在于它会自动检测您正在编写的变量类型并相应地将它们转换为文本。在这种情况下,这种自动化并不是你所期望的。您传递的是char *类型的值,这意味着,流i / o将假定您要输出字符串。所以它会写出你未初始化的数组中的任何内容,直到它找到一个NUL字符。



你打算做的是将char *写成地址值,你就是可以说:


The good thing about formatted stream i/o is that it automatically detects the type of variable your are writing and converts them to text accordingly. In this case this automatism however doesn''t do what you expected. You are passing a value of type char* and that means, the stream i/o will assume that you want to output a character string. So it writes whatever is in your yet uninitialized array until it finds a NUL-character.

What you intended to do is write that char* as address value, which you may accomplish by saying:

cout << (size_t) &array[j][k] << endl;



这会让你有意识清楚,你会得到你想要的。顺便说一句,你可以保留你的printf语句,并且它们在C ++中也可以正常工作。


That will make you intention clear and you get what you want. By the way, you could have left your printf statements as they were and they would have worked fine in C++ also.


这篇关于用于2d char数组的C和C ++分配内存。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆