How to use strcat() function?

后端 未结 2 708
离开以前
离开以前 2021-01-15 06:15

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

#include 
#include 

int main(int argc, const char *argv[])          


        
相关标签:
2条回答
  • 2021-01-15 06:52

    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;
    }
    
    0 讨论(0)
  • 2021-01-15 06:54

    Both of the code snippet invoke undefined behavior. In the first snippet s1 has not enough space to hold any other character than six characters.
    In the second snippet you are trying to modify a string literal. Any attempt to modify a string literal lead to undefined behavior.

    Read strcat man page:

    [...] The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

    0 讨论(0)
提交回复
热议问题