I want to initialize string in C to empty string. I tried:
string[0] = "";
but it wrote
"warning: assignment makes integer from pointer without a cast"
How should I do it then?
If you want to zero the entire contents of the string, you can do it this way: memset(buffer,0,strlen(buffer));
In practice, NULL is a constant equivalent to 0 , or "\0" . This is why you can set a string to NULL using: char *a_string = '\0'; Download my free C Handbook!
In addition to Will Dean's version, the following are common for whole buffer initialization:
char s[10] = {'\0'};
or
char s[10]; memset(s, '\0', sizeof(s));
or
char s[10]; strncpy(s, "", sizeof(s));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With