我正在尝试理解下面的代码,但是Xcode在第2行到最后一行给出了一个错误,说“线程1:信号SIGABRT”
char string1[4] = "abc";
char string2[4] = "def";
printf("%s \n%s\n", string1, string2);
strcpy(string1, string1+1); // Xcode points here, "Thread 1: signal SIGABRT"
printf("%s \n%s", string1, string2);
发生了什么事?我希望它打印
abc
def
bc
def
但它显然会中途停止。
答案 0 :(得分:3)
引自strcpy文档:源字符串和目标字符串不应重叠,因为行为未定义。
您的来源和目的地重叠。
使用memmove()代替重叠数据。