我声明了一个char变量数组,如下所示:

char word[256];
char plural[256];

它接受main函数中的输入并复制到多个变量,如下所示:
scanf("%s",&word);
strcpy(plural,word);

我提供的输入是“Baby”。
main方法通过将两个变量作为参数传递来调用另一个函数复数,如下所示:
void pluralize(word,plural);

下面是我想用复数方法做的事情:
void pluralize(char word[], char plural[]){
    char textToBeAdded[] = "IES";
    int i = strlen(plural);
    plural[i-1] = '\0';
    plural = strcat(plural, textToBeAdded);
    printf("Word is %s and plural is %s", word, plural);
    printf("\nRule is 1\n");
}

我没有使用char*char[],所以应该可以修改。但它显示了一个分段运行时错误。为什么我做错了什么?

最佳答案

一个问题是这句话:

scanf("%s",&word);

word是一个chars数组。因此要读入它,您只需:
scanf("%s",word);

关于c - 为什么将char数组作为参数传递给函数并尝试在函数内部进行修改会显示段错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55684228/

10-11 23:07