Append Char To String in C?

前端 未结 9 1131
滥情空心
滥情空心 2020-11-27 16:17

How do I append a single char to a string in C?

i.e

char* str = \"blablabla\";
char c = \'H\';
str_append(str,c); /* blablablaH */
9条回答
  •  星月不相逢
    2020-11-27 16:31

    I do not think you can declare a string like that in c. You may only do that for const char* and of course you can not modify a const char * as it is const.

    You may use dynamic char array but you will have to take care of the reallocation.

    EDIT: in fact this syntax compiles correctly. Still you can should not modify what str points to if it is initialized in the way you do it (from string literal)

提交回复
热议问题