Initialize a string in C to empty string

前端 未结 9 1522
甜味超标
甜味超标 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:19

    You want to set the first character of the string to zero, like this:

    char myString[10];
    myString[0] = '\0';
    

    (Or myString[0] = 0;)

    Or, actually, on initialisation, you can do:

    char myString[10] = "";
    

    But that's not a general way to set a string to zero length once it's been defined.

提交回复
热议问题