Your string char *str = '\0'; actually sets str to (equivalent) NULL. This is because '\0' in C is an integer with the value 0, which is a valid null pointer constant. It is extremely confusing, though :-)
Creating str (pointer to) an empty string is done using str = ""; (or using str = "\0"; ), which will make str a point in an array of two zero bytes).
Note. Do not confuse your declaration with the instruction on line 3 here
char *str; *str = '\0';
which does something completely different: it sets the first character of the string, which str points to a null byte, effectively making str point to an empty string.
Nitpick terminology: strings cannot be set to NULL; string C is an array of characters, somewhere a NUL character. Without a NUL character, it is just an array of characters and should not be passed to functions that expect strings (pointers). Pointers, however, are the only objects in C that can be NULL. And do not confuse the NULL macro with the NUL character :-)
source share