简体   繁体   中英

Concatenating two strings in C and placing the result into a third string

This is purely for self-interest and is not a homework assignment.

#include <stdio.h>

int main(void)
{
    char* str3;

    char* str1 = "Hello";
    char* str2 = "World!";

    while(*str1) str1++;
    while(*str1++ = *str2++);

    return 0;
}

I am attempting to develop a better understanding of C pointers and in doing so I would like to concatenate two strings and place the result into a third string. The (incomplete) code above results in a segfault and I'm not sure why. Isn't it possible to loop over the value referenced by a pointer and copy the data to another address?

Edit:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char* str1 = "Hello";
    char* str2 = "World!";

    char *str3 = malloc(strlen(str1) + strlen(str2) + 1);

    while(*str1) *str3++ = *str1++;
    while(*str2) *str3++ = *str2++;

    puts(str3);

    return 0;
}

The new attempt is above, and while not functional, are there "obvious" items I need to fix?

    while(*str1) str1++;

This advances str1 until it points to the terminating zero byte at the end of the string constant.

    while(*str1 = *str2++);

This modifies the terminating zero byte at the end of the string, but the string is a constant and so can't be modified.

If you want to assemble a string in memory, you need to allocate some space to do that in or use functions that do so. You could do:

char *new_string = malloc(strlen(str1) + strlen(str2) + 1);
strcpy(new_string, str1);
strcat(new_string, str2);

The first line allocates enough space to hold str1 's contents, str2 's contents, and the terminating zero byte.

  int main(void)
  {
      char* str1 = "Hello";
      char* str2 = "World!";
      // allocate one more byte for string terminate cher ('\n')
      int size = strlen(str1) + strlen(str2);
      char* str3 = (char*)malloc(size + 1);
      char* str_mod = str3;

      while( (*str_mod++ = *str1++) != '\0');
      str_mod--;
      while( (*str_mod++ = *str2++) != '\0');

      printf ( "%s", str3);
      free (str3);

      return 0;
  }

The constant strings are not modifyable. You are not modifying str3. You need to

  1. get the length of str1 and str2.
  2. malloc length of str1 + length str2 + 1 and assign to str3
  3. You can use while loops if you like for the copy, or you can use strcpy and strcat to copy the strings to str3.

Since you are trying to learn, I am trying not to write the code for you.

int main (void){
    char* str1 = "Hello";
    char* str2 = "World";

    int size1 = strlen(str1);
    int size2 = strlen(str2);
    int i = 0;

    char* out = malloc(sizeof(char)*(size1+size2)+1);

    for (i = 0; i < size1; i++){
        out[i] = str1[i];
    }
    for (i = 0; i < size2; i++){
        out[i+size1] = str2[i];
    }
    out[strlen(out)-1] = \0;
    //out is a string.
    //dont forget to free when you are done.  free(out);

}

Its been a few months since i did C, but this would work. my syntax might be slightly off.

2nd attempt in question misses putting string terminating null to end of str3 . So puts reads beyond end of data, prints garbage and may even crash if there is no 0 byte before reading invalid address. Add *str3 ='\\0'; after loops.

Additionally, you modify str3 and lose start of string. Add one more variable, keep the pointer returned by malloc , and pass that to puts . Current code will start printing at the end of the new string.

Then when you have pointers to string literals, make them pointers to const char, because usually string literals are in read only memory area:

const char* str1 = "Hello";
const char* str2 = "World!";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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