How to implement the concatenation of character array pointers in the C language?

In C language, string concatenation with character arrays can be accomplished by using the strcat() function. The strcat() function is used to append one string to the end of another string. Here is a simple example code:

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

int main() {
    char str1[50] = "Hello, ";
    char str2[] = "World!";
    
    printf("Before concatenation: %s\n", str1);
    
    strcat(str1, str2);
    
    printf("After concatenation: %s\n", str1);
    
    return 0;
}

In the example above, we first define two character arrays str1 and str2, and then use the strcat() function to concatenate str2 to the end of str1. The final output is Hello, World! It is important to note that when using the strcat() function, make sure that the target character array has enough space to store the concatenated string, otherwise it may lead to a memory overflow.

bannerAds