How to use strcat() function?

后端 未结 2 730
离开以前
离开以前 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 
    #include 
    
    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 
    #include 
    
    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 
    #include 
    
    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 
    #include 
    #include 
    
    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;
    }
    

提交回复
热议问题