在C语言中使用字符串数组

在C语言中使用字符串数组,c,malloc,C,Malloc,我只是在练习理解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){

我只是在练习理解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字符串的位置,该字符串位于只读内存中,您应该更改参数的顺序。

您阅读了strcpy的哪些文档?不确定我在哪里犯了错误调试器会将您指向导致程序崩溃的行。似乎您所需要的只是一个好的命令。顺便说一下,几乎任何教程或书籍都会告诉您如何使用strcpy。strcpyhello\n,args[c];=>strcpyargs[c],您好\n;哦,不,char**args;将args定义为指向char的指针。C中也没有数据类型字符串。