关于指向指针的指针,我想知道它是否适合以下内容:
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/