Search code examples
cstringpointersc-stringsstrcat

How to use strcat() function?


I am very new in C language. I was trying to use strcat function.

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

int main(int argc, const char *argv[]) {
    char s1[] = "12345";
    char s2[] = "abcde";

    strcat(s1, s2);

    puts(s1);
    puts(s2);
    return 0;
}

This one ran normally,but

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

int main(int argc, const char *argv[]) {
    char* s1 = "12345";
    char* s2 = "abcde";

    strcat(s1, s2);

    puts(s1);
    puts(s2);
    return 0;
}

the last one failed to return a result. Why did the two different ways of declaration return different results in the strcat function. Thanks in advance.


Solution

  • In C the function strcat does not create a new character array containing concatenated strings. It appends characters from the second string to the first string of the first character array provided that it has enough elements to store the new characters. Otherwise the function will try to overwrite the memory beyond the character array that results in undefined behavior.

    So a valid use of the function in the first program can look the following way

    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, const char *argv[]) {
        char s1[11] = "12345";
        char s2[] = "abcde";
    
        strcat(s1, s2);
    
        puts(s1);
        puts(s2);
        return 0;
    } 
    

    In this program the character array is declared as having 11 elements. Thus it is able to accommodate the appended string "abcde".

    In the second program there is an attempt to modify the string literal pointed to by the pointer s1. String literals in C and C++ are immutable. Any attempt to change a string literal results in undefined behavior even though in C opposite to C++ string literals have types of non-constant character arrays.

    From the C Standard (6.4.5 String literals)

    7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

    So in the second program you again need to use a character array with enough elements. For example

    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, const char *argv[]) {
        char s1[11] = "12345";
        char* s2 = "abcde";
    
        strcat(s1, s2);
    
        puts(s1);
        puts(s2);
        return 0;
    }
    

    Or you could use either a Variable Length Array (VLA) if the compiler supports them or dynamically allocate an array. For example

    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, const char *argv[]) {
        char *s1 = "12345";
        char* s2 = "abcde";
        char s3[strlen( s1 ) + strlen( s2 ) + 1];    
    
        strcpy( s3, s1 );
        strcat( s3, s2 );
    
        puts(s1);
        puts(s2);
        puts(s3);
    
        return 0;
    }
    

    Or

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, const char *argv[]) {
        char *s1 = "12345";
        char* s2 = "abcde";
        char *s3 = malloc( strlen( s1 ) + strlen( s2 ) + 1 );    
    
        if ( s3 != NULL )
        {
            strcpy( s3, s1 );
            strcat( s3, s2 );
    
            puts(s1);
            puts(s2);
            puts(s3);
        }
    
        free( s3 );
    
        return 0;
    }