简体   繁体   English

使用指针进行字符串连接中的分段错误

[英]Segmentation fault in string concatenation using pointers

I'm trying to concatenate two strings stored in character pointers, but am doing something wrong. 我试图连接存储在字符指针中的两个字符串,但我做错了。 Could someone point out what it is, please? 有人可以指出它是什么吗? Also, I'm not using any built in functions purposely. 另外,我没有故意使用任何内置功能。

int main()
{
    char *a = "abc";
    char *b = "def";
    char *c;

    while(*a != '\0')
    {
        *c = *a;
        a++;
        c++;
    }

    while(*b != '\0')
    {
        *c = *b;
        b++;
        c++;
    }

    *c = '\0';
    c -= 6;

    while(*c!= '\0')
    {
        printf("%c", *c);
        c++;
    }

    return 0;

} }

You haven't allocated any memory for c 你没有为c分配任何内存

b and c get memory allocated statically, when you do: 当你这样做时, bc静态分配内存:

char *a = "abc";
char *b = "def";

But c doesn't have that, so you would need to allocate memory using something like: 但是c没有那个,所以你需要使用类似的东西来分配内存:

char *c = malloc (x);

where x is the total length of the character array you'd need to accomodate the characters you wish to insert (plus 1, for the terminating NULL). 其中x是您需要容纳要插入的字符的字符数组的总长度(加1,终止NULL)。 You'd also need to remember to free () it somewhere down the line. 你还需要记住在某个地方free ()它。

As you're not doing any allocation, this line: 由于你没有做任何分配,这一行:

*c = *a;

will produce undefined behaviour. 将产生未定义的行为。

c is uninitialized. c未初始化。 You must initialize it before copying values. 您必须在复制值之前对其进行初始化。 For example: 例如:

char *c = malloc(strlen(a) + strlen(b) + 1); /* plus 1 for terminating null byte */
if (!c) {
    perror("malloc");
    exit(EXIT_FAILURE);
}

Other problem is you are not incrementing c when you print: 其他问题是您在打印时没有增加c

   while(*c!= '\0')
    {
        printf("%c", *c);
        c++;
    }

Note that this statement 请注意这个声明

c -= 6;

is fine here but not very readable. 这里很好,但不是很可读。 You are better off using a temporary pointer to save the initial position of c , so that you don't need to do this. 最好使用临时指针来保存c的初始位置,这样就不需要这样做了。

If you can't use standard strlen function, then it's straight-forward to implement it yourself. 如果你不能使用标准的strlen函数,那么你可以直接自己实现它。

 char *c; 

With this line, you define a variable c that is a pointer to a char . 使用此行,您可以定义一个变量c ,它是指向char的指针。 It's clear your idea is to store in it the concatenation of the strings a and b ; 很明显,你的想法是在其中存储字符串ab的串联; but, to do that, you need to have some memory available for the result 'a+b' (concatenation) string. 但是,要做到这一点,你需要为结果'a+b' (串联)字符串提供一些内存

What you have to do is allocating enough memory for the destination string, counting the characters in the two source strings to concatenate, plus the terminating NUL ( \\0 ). 你要做的是为目标字符串分配足够的内存,计算要连接的两个源字符串中的字符,以及终止NUL\\0 )。 In your case, you need 3 + 3 + 1 = 7 char s for the result string. 在您的情况下,结果字符串需要3 + 3 + 1 = 7 char

You can either allocate them on the stack, like this: 您可以在堆栈上分配它们,如下所示:

char result[7];

Or dynamically allocate using malloc() . 或者使用malloc()动态分配。 In this case, you also need to free the memory invoking free() . 在这种情况下,您还需要释放调用free()的内存。

You are are getting the error because the pointer c is not initialized. 您正在收到错误,因为指针c未初始化。

int main()
{
char *a = "abc";
char *b = "def";
char x ;
char *c = &x; //initializing pointer

while(*a != '\0')
{
    *c = *a;
    a++;
    c++;
}

while(*b != '\0')
{
    *c = *b;
    b++;
    c++;
}

*c = '\0';
c -= 6;

while(*c!= '\0')
{
    printf("%c", *c++);//increment pointer
}
return 0;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM