Click here to Skip to main content
15,393,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I have tried, see the code below, to concatenate 2 pointer strings with the strcat() function. It didn't work.

// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (p. 400). Pearson Education. Kindle Edition.
//
// 7. ON YOUR OWN: Write a function that accepts two strings. Use the malloc() function to allocate enough memory to hold
// the two strings after they have been concatenated (linked). Return a pointer to this new string. For example, if you pass
// "Hello" and "World!", the function returns a pointer to "Hello World!". Having the concatenated value be the third string
// is easiest. (You might use your answers from exercises 5 and 6.)

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

  int main( void )
  {
    char *A = "Hello";
    char *A2 = " ";
    char *A3 = "World!";

    strcat(A,A2);
    printf("\n%s\n",A);

    return 0;
 }


How do I concatenate 2 pointer strings correctly?

What I have tried:

I have tried to change from
printf("\n%s\n", A);
to
printf("\n%s\n",A);.
.

I have also tried with:
printf("\n%s\n", strcat(A,A2));
.
Posted
Updated 9-Oct-21 3:16am

The best way is to do use malloc to allocate the memory to copy it into, then use memcopy - as Richard says, trying to change a constant string can crash your app.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
    {
    printf("Hello World\n");
    char *A = "Hello";
    char *B = " World";
    int lenA = strlen(A);
    int lenB = strlen(B);
    char *A3 = (char*) malloc(lenA + lenB + 1);
    memcpy(A3, A, lenA);
    memcpy(A3 + lenA, B, lenB);
    A3[lenA + lenB] = '\0';
    printf("\n%s\n",A3);

    return 0;
    }

malloc provides a way to allocate memory of a variable size - so it's easier to write a function that can be called repeatedly to do the copy to fresh memory:
char* AppendStrings(const char *A, const char*B)
    {
    int lenA = strlen(A);
    int lenB = strlen(B);
    char *C = (char*) malloc(lenA + lenB + 1);
    memcpy(C, A, lenA);
    memcpy(C + lenA, B, lenB);
    C[lenA + lenB] = '\0';    
    return C;
    }
Then you can call it with whatever you want:
char * C  = AppendStrings(A, B);
printf("\n%s\n",C);
char * D  = AppendStrings("Hello World!\n", "This is a test");
printf("\n%s\n",D);
   
Comments
Rick York 9-Oct-21 12:03pm
   
You can also use calloc which will zero the allocated memory so the terminating null character is there automatically.
OriginalGriff 9-Oct-21 12:19pm
   
I tend to forget calloc - it's been a long time since I wrote any non-embedded C code ... so I haven't called malloc or calloc in the real world for decades! :laugh:
KarstenK 9-Oct-21 12:45pm
   
It has to be mentioned that the allocated memory has to be freed when done, else it is leaking.
You are concatenating a single space to A so it probably appears not to work (you forgot A3). However, trying to write into a constant string is never guaranteed and you should not do it. You should create a new char buffer and copy the text into that. Something like:
C++
char *A = "Hello";
char *A2 = " ";
char *A3 = "World!";

char buffer[32]; // make sure this is big enough for the final text
strcpy(buffer, A);    // copy the first string into the buffer
strcat(buffer, A2);   // concatenate the space
strcat(buffer, A3);   //     and the final string
printf("\n%s\n", buffer);
   
Comments
Mieczyslaw1683 9-Oct-21 9:05am
   
Thank you for the answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900