What is the difference between str==NULL and str[0]=='\0' in C?
I want to know the difference between str == NULL
and str[0] == '\0'
:
int convert_to_float(char *str, double *num)
{
if ((str == NULL) || (str[0] == '\0'))
return(-1);
*num = strtod(str, (char **)NULL);
return(0);
}
I'm using gcc on Linux.
str==NULL
tells you whether the pointer is NULL.
str[0]=='\0'
tells you if the string is of zero-length.
In that code, the test:
if ((str == NULL) || (str[0] == '\0'))
is used to catch the case where it is either NULL or has zero-length.
Note that short-circuiting plays a key role here: The point of the test is to make sure that str
is a valid c-string with length at least 1.
- The second test
str[0] == '\0'
will only work ifstr
is not NULL. - Therefore, the first test
str == NULL
is needed to break out early whenstr
is NULL.
It's important to remember that str
isn't really a "a string", but rather a pointer to the memory location where a char
(a part of a string) is stored.
Next, we have to understand how the compiler sees all of these items. Let's look at their types:
-
str
is of typechar *
(literally, "pointer to achar
") -
NULL
is a null pointer constant (at least on my system, it's((void*)0)
) -
'\0'
is a character constant (it's actually of typeint
, but don't worry about that; it's generally used in a context that requires achar
value)
See the *
in char *
and void *
? That tells the compiler that these are pointer types (which is a fancy way of saying that variables of this type don't hold the value, they just point at it). So when the compiler sees char *str
, it knows that you might ask to do something like *str
or str[0]
(which both do the same thing). We'll get back to that, later.
You see, when you write str
in a C program, the compiler knows that a variable called "str" is stored in a memory location, for example 0x0001. The code it generates goes to 0x0001 and fetches the value. That way, if you do something like
str + 1
Then the compiler will generate code that looks something like:
fetch the value from where str is stored (0x0001)
add 1 to that value
Which is something I'm sure you know. So now it should be obvious what this line says:
str == NULL
Since NULL
is a null pointer constant, that line tests whether str
is a null pointer (i.e., a pointer that doesn't point to anything).
So the compiler typically generates code like this:
fetch the value from where str is stored
check if that value is 0
Remember now, if you please, that we told the compiler that str
is really a pointer type. So we're allowed to write this:
*str
And that makes the compiler generate this:
fetch the value from where str is stored
now use that value as a memory address and fetch what is stored there
So if str held 0x0200, then we would get the value from the memory address 0x0200. Note that the compiler doesn't really care if a string is really stored there or not.
(I'm going to assume you know that str[0]
is the same as *str
. It makes it easier to explain what's going on.)
How about this, then?
*str == '\0'
So that line is really, in effect:
*str == (char) 0
Which makes the compiler generate this:
fetch the value from where str is stored
now use that value like a memory address and fetch the char that is stored there
check if the value of that fetched char is 0
To summarize:
- Writing
str == NULL
tells you whether the pointerstr
is pointing at nothing. - Writing
*str == '\0'
tells you whether the pointerstr
is pointing at a an empty string (actually, pointing at a memory location holding a zero).
(A "string" is, by definition, "a contiguous sequence of characters terminated by and including the first null character", so if the very first character of a string is '\0'
, then the string is an empty string.)