请教一个c语言动态创建二维数组的问题
当前明确的创建二维数组并赋值的方法可以用
char **charTemp = (char **)malloc(sizeof(char *) * 3);
for (int i = 0; i < 3; i++) {
charTemp[i] = (char *)malloc(sizeof(char) * 20);
charTemp[i] = "this is a test";
}
for (int i = 0; i < 3; i++) {
printf("\r\n %s", charTemp[i]);
}
这样是可以打印出三个this is a test;
for (int i = 0; i < 3; i++) {
charTemp[i] = (char *)malloc(sizeof(char) * 20);
charTemp[i] = "this is a test";
}
for (int i = 0; i < 3; i++) {
printf("\r\n %s", charTemp[i]);
}
这样是可以打印出三个this is a test;
但是用如下方式动态创建的二维数组,我如何在第二行,第三行赋值this is a test呢?
char **charTemp = (char **)malloc(sizeof(char *) * 3);
for (int i = 0; i < 3; i++) {*charTemp = (char *)malloc(sizeof(char) * 20);
*charTemp = "this is a test";
}
#C语言编程##C/C++#