concatenate char array in C

后端 未结 7 1692
故里飘歌
故里飘歌 2020-12-05 02:15

I have a a char array:

char* name = \"hello\";

I want to add an extension to that name to make it

hello.txt
7条回答
  •  孤城傲影
    2020-12-05 02:36

    Have a look at the strcat function.

    In particular, you could try this:

    const char* name = "hello";
    const char* extension = ".txt";
    
    char* name_with_extension;
    name_with_extension = malloc(strlen(name)+1+4); /* make space for the new string (should check the return value ...) */
    strcpy(name_with_extension, name); /* copy name into the new var */
    strcat(name_with_extension, extension); /* add the extension */
    

提交回复
热议问题