我只是在做一些练习代码,但我不知道这个顽固的线程1:


  信号SIGABRT错误。


int main(){
    char diet[] = "vegan";

    printf("Because of health concerns, my diet is now %s.\n", diet);

    strcpy(diet, "whatever");

    printf("Before the health concerns, my diet was %s.\n", diet);


    return 0;
}

最佳答案

strlen(“ whatever”)> strlen(“ vegan”)=未定义的行为。

为什么您认为需要复制字符串。您可以这样做:

int main(){
    char *diet = "vegan";
    printf("Because of health concerns, my diet is now %s.\n", diet);
    diet = "whatever";
    printf("Before the health concerns, my diet was %s.\n", diet);
    return 0;
}

10-08 11:46