Convert char array to string use C

后端 未结 3 803
迷失自我
迷失自我 2020-12-08 17:21

I need to convert a char array to string. Something like this:

char array[20];
char string[100];

array[0]=\'1\';
array[1]=\'7\';
array[2]=\'8\';
array[3]=\'         


        
3条回答
  •  悲哀的现实
    2020-12-08 17:46

    Assuming array is a character array that does not end in \0, you will want to use strncpy:

    char * strncpy(char * destination, const char * source, size_t num);
    

    like so:

    strncpy(string, array, 20);
    string[20] = '\0'
    

    Then string will be a null terminated C string, as desired.

提交回复
热议问题