9

Is setting a string to '\0' the same thing as setting a string to NULL in other languages? Or... does setting a string to '\0' mean that the string is simply just empty?

char* str = '\0'

I have different functions to use for null strings and empty strings, so I don't want to accidentally call one of my empty string functions on a null string.

1
  • 5
    It's good practice to attach the * character to the variable name instead of the type name in order to avoid confusion. Consider char* p1, p2;. This defines a pointer-to-char p1 and a char p2, but it reads as if both p1 and p2 were of type pointer-to-char. Commented Apr 10, 2011 at 0:43

2 Answers 2

19

Your line char *str = '\0'; actually DOES set str to (the equivalent of) NULL. This is because '\0' in C is an integer with value 0, which is a valid null pointer constant. It's extremely obfuscated though :-)

Making str (a pointer to) an empty string is done with str = ""; (or with str = "\0";, which will make str point to an array of two zero bytes).

Note: do not confuse your declaration with the statement in line 3 here

char *str;
/* ... allocate storage for str here ... */
*str = '\0'; /* Same as *str = 0; */

which does something entirely different: it sets the first character of the string that str points to to a zero byte, effectively making str point to the empty string.

Terminology nitpick: strings can't be set to NULL; a C string is an array of characters that has a NUL character somewhere. Without a NUL character, it's just an array of characters and must not be passed to functions expecting (pointers to) strings. Pointers, however, are the only objects in C that can be NULL. And don't confuse the NULL macro with the NUL character :-)

Sign up to request clarification or add additional context in comments.

7 Comments

@Matteo are you sure that's only if it's in DOUBLE quotes, and that SINGLE quotes make it a char?
Yes, but you have to make a distinction between a declaration char *str = '\0'; and the statement *str = '\0';. They do very different things.
@glowcoder: sorry, brainfart. I read *str='\0' instead of char * str='\0'. You got my +1 :)
@Matteo: no biggie; it was easy to confuse single and double quotes and I had to use my eagle eyes as well :-) Thanks for the vote, I appreciate it.
@Bionix No, that doesn't work, because s is not initialized. It points to no memory. You would need something like s = malloc(42); strcpy (s, src);.
|
1

No, in this case you're pointing to a real (non-null) string with a length of 0. You can simply do the following to set it to actual null:

char* str = NULL;

4 Comments

No, it doesn't. If it said "\0", then it would.
Ahhh. Stupid single quotes. Good call!
@CatPlusPlus: According to the accepted answer, that would be "an array of two zero bytes."
@rvighne Uh so? Also that's still a string of length zero, because dumb C strings are NUL-terminated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.