我只是在练习理解 C 中的动态分配。我收到了段错误错误。我不确定我在哪里犯了错误。
int wordcount = 5;
char **args = (char**)malloc(wordcount * sizeof(char*));
for ( int i = 0; i < wordcount; i++) {
args[i] = (char*)malloc(167 * sizeof(char));
}
int c=0;
while(c < wordcount){
strcpy("hello\n", args[c]);
c++;
}
最佳答案
您正在尝试将 args[c]
复制到 "hello\n"
字符串的位置,该字符串位于只读存储器中,您应该更改顺序参数。
关于c - 在C中使用字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50427514/