How does one represent the empty char?

前端 未结 9 1339
闹比i
闹比i 2020-12-22 18:06

I\'m currently writing a little program but I keep getting this error when compiling

error: empty character constant

I realize i

9条回答
  •  庸人自扰
    2020-12-22 18:27

    There are two ways to do the same instruction, that is, an empty string. The first way is to allocate an empty string on static memory:

    char* my_variable = "";
    

    or, if you want to be explicit:

    char my_variable = '\0';
    

    The way posted above is only for a character. And, the second way:

    #include 
    char* my_variable = strdup("");
    

    Don't forget to use free() with this one because strdup() use malloc inside.

提交回复
热议问题