<分区>
Possible Duplicates:
string literal in c
Assigning strings to arrays of characters
我得到了以下代码:
char result[3][4];
result[0] = "horse";
result[1] = "pig";
result[2] = "chicken";
它不会编译说 Incompatible types in assignment
。我做错了什么?
<分区>
Possible Duplicates:
string literal in c
Assigning strings to arrays of characters
我得到了以下代码:
char result[3][4];
result[0] = "horse";
result[1] = "pig";
result[2] = "chicken";
它不会编译说 Incompatible types in assignment
。我做错了什么?
最佳答案
char 是单个字符,例如 'h'
你想要的是字符串指针。
char *result[3];
请记住,如果您这样做,您将无法更改单个字符串的内容(它是一个常量并存储在不同的位置。)
你也可以这样做
char *result[3];
result[0] = malloc(100 * sizeof(char));
strcpy(result[0],"horse"); // or strncpy(result[0],5,"horse")
etc
关于C 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4769739/