创建动态多维指针数组(char**)时出现运行时错误
我试图创建动态多维数组char**变量来存储三个字符串,但运行时出现未知错误创建动态多维指针数组(char**)时出现运行时错误,c,arrays,pointers,memory-management,C,Arrays,Pointers,Memory Management,我试图创建动态多维数组char**变量来存储三个字符串,但运行时出现未知错误 // Allocate memory for three strings char **str = (char**) malloc(sizeof(char*)*3); for(int i=0;i<3;i++) str[i] = (char*) malloc(20); // Assign value to each string item strcpy(str[0], "LionKing"); strcp
// Allocate memory for three strings
char **str = (char**) malloc(sizeof(char*)*3);
for(int i=0;i<3;i++)
str[i] = (char*) malloc(20);
// Assign value to each string item
strcpy(str[0], "LionKing");
strcpy(str[1], "Godzilla");
strcpy(str[2], "Batman");
// Print the strings
for(int i=0;i<3;i++)
printf("%s\n", *str[i]);
// Free the memory of the three strings
for(int i=0;i<3;i++)
free(str[i]);
// Free the memory of the main pointer
free(str);
//为三个字符串分配内存
char**str=(char**)malloc(sizeof(char*)*3);
对于(int i=0;iprintf(“%s\n”,*str[i]);
应该是printf(“%s\n”,str[i]);
*str[i]
是一个字符
,但是%s
需要一个字符*
。编译器应该对此发出警告。如果没有,请在编译器中启用警告,并注意它们。激活编译器中的警告并读取它们。