本文介绍了strcat的模拟在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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在xstrcat(S1 = 0x400696地方,S2 = 0x400696地方)
    在strcat.c:23
    23 * S1 = S2 *;

Error output:Program received signal SIGSEGV, Segmentation fault. 0x000000000040055d in xstrcat (s1=0x400696 "place", s2=0x400696 "place") at strcat.c:23 23 *s1=*s2;

我不能够在内存中写。谁能告诉我为什么我收到此错误。 ?

I am not able to write in that memory. Can anyone please tell why i am getting this error. ?

推荐答案

STR1 是一个字符串字面它是不确定的行为写入到该位置。这将是有效的创建和写入 STR1 如果它是这样定义的:

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";

这篇关于strcat的模拟在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:46