c++ - 声明和初始化指针到指针字符数组

标签 c++ initialization pointer-to-pointer

关于指向指针的指针,我想知道它是否适合以下内容:

    char** _charPp = new char*[10];

     for (int i = 0; i < 10; i++)
    ´   _charPp[i] = new char[100];

也就是说 - 如果我想要一个指向 10 个字符数组的指针。

问题 - 现在可以了吗,还是我必须对每个字符数组进行某种初始化?那么 - 我该怎么做?

稍后我会在程序中用某些字符填充这些数组,但我怀疑数组应该用之前的值初始化,例如“0”

最佳答案

是的,看起来很合适,

int arraySizeX = 100;
int arraySizeY = 100;
char** array2d = new char*[arraySizeX] // you've just allocated memory that
                                       // holds 100 * 4 (average) bytes
for(int i = 0; i < arraySizeX; ++i)
{
    array2d[i] = new char[arraySizeY] // you've just allocated a chunk that 
                                       //holds 100 char values. It is 100 * 1 (average) bytes
    memset(array2d[i], 0, arraySizeY) // to make every element NULL
}

I will later on in the program fill these arrays with certain chars but i suspect that the arrays should be initialized with values before such as "0"

不,C++ 中没有“默认值”。您需要将 0 分配给指针以使其为空,或使用 memset() 对数组执行此操作。

关于c++ - 声明和初始化指针到指针字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23606164/

相关文章:

c++ - XPATH 包含(字符串,字符串)不工作

c++ - 每当按键时终止程序

c - 指向指针地址的指针

Xcode 中的 C++ 单例

c# - C# 中 C++ const size_t 的等价物是什么?

c++ - 数组初始化 - 有条件地

C++11 初始化列表、数组和枚举的乐趣

swift - 在 super.init 调用之前初始化非可选属性,无需重复代码

c - 使用 -> 运算符访问指向结构指针的指针

c - 指向指针的指针和指向二维数组的指针之间的区别