concatenate char array in C

后端 未结 7 1697
故里飘歌
故里飘歌 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:31

    asprintf is not 100% standard, but it's available via the GNU and BSD standard C libraries, so you probably have it. It allocates the output, so you don't have to sit there and count characters.

    char *hi="Hello";
    char *ext = ".txt";
    char *cat;
    
    asprintf(&cat, "%s%s", hi, ext);
    

提交回复
热议问题