1
#include<stdio.h>

main()
{
    char *str1="beautiful";
    char *str2="place";
    xstrcat(str1,str2);
}

xstrcat(char *s1,char *s2)
{

    char *temp;
    temp=s1;

    while(*s1!='\0')
    {
        s1++;
    }
    while(*s2!='\0')
    {
        s1++;
        *s1=*s2;
        s2++;
    }
    s1++;
    *s1='\0';
    printf("\n%s",temp);
}

错误输出:程序接收信号 SIGSEGV,分段错误。0x000000000040055d in xstrcat (s1=0x400696 "place", s2=0x400696 "place") at strcat.c:23 23 *s1=*s2;

我无法在那个记忆中写作。谁能告诉我为什么会收到此错误。?

4

1 回答 1

0

str1 is a string literal it is undefined behavior to write to that location. It would be valid to create and write to str1 if it was defined like this:

char str1[100] = "beautiful";
于 2013-05-31T15:43:52.293 回答