Search code examples
cappendc-stringsstrcpystrcat

Appending element into an array of strings in C


I have an array of strings with a given size, without using any memory allocation, how do I append something into it?

Say I run the code, its waiting for something you want to enter, you enter "bond", how do I append this into an array ? A[10] ?


Solution

  • If the array declared like

    char A[10];
    

    then you can assign string "bond" to it the following way

    #include <string.h>
    
    //...
    
    strcpy( A, "bond" );
    

    If you want to append the array with some other string then you can write

    #include <string.h>
    
    //...
    
    strcpy( A, "bond" );
    strcat( A, " john" );