我只是练习理解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++;
}
答案 0 :(得分:1)
您正在尝试将args[c]
复制到"hello\n"
字符串的位置,该字符串位于只读内存中,您应该更改参数的顺序。