Initialize a string in C to empty string

前端 未结 9 1570
甜味超标
甜味超标 2020-12-24 15:06

I want to initialize string in C to empty string. I tried:

string[0] = \"\"; 

but it wrote

\"warning: assignment makes inte         


        
9条回答
  •  [愿得一人]
    2020-12-24 15:28

    It's a bit late but I think your issue may be that you've created a zero-length array, rather than an array of length 1.

    A string is a series of characters followed by a string terminator ('\0'). An empty string ("") consists of no characters followed by a single string terminator character - i.e. one character in total.

    So I would try the following:

    string[1] = ""

    Note that this behaviour is not the emulated by strlen, which does not count the terminator as part of the string length.

提交回复
热议问题