What is the difference between str==NULL and str[0]=='\0' in C?

CPointersGccCstring

C Problem Overview


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.

C Solutions


Solution 1 - C

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 if str is not NULL.
  • Therefore, the first test str == NULL is needed to break out early when str is NULL.

Solution 2 - C

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 type char * (literally, "pointer to a char")
  • NULL is a null pointer constant (at least on my system, it's ((void*)0))
  • '\0' is a character constant (it's actually of type int, but don't worry about that; it's generally used in a context that requires a char 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 pointer str is pointing at nothing.
  • Writing *str == '\0' tells you whether the pointer str 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.)

Solution 3 - C

Essentially

  • str == NULL determines if str is a NULL pointer
  • str[0] == '\0' determines if str is a 0 length c style string

When you combine them you are checking if it NULL or empty. This allows the function to eliminate both forms of empty data at the start of the method

Solution 4 - C

str == NULL checking str is NULL-pointer (pointer to nowhere)

str[0] == '\0' (if not NULL-pointer) checking first str element has 0-value (string without characters only 0-terminated)

Solution 5 - C

str==NULL tells you whether the string is NULL.

*str=='\0' tells you if the string is of zero-length.

Note: This answer is a play on Mystical's 15 second answer which had str=='\0'. Of course changes made in the first 3 or 4 minutes aren't shown and he fixed it ಠ_ಠ.

Solution 6 - C

str == NULL means "str points to memory address zero" (or whatever address is NULL on your system). Typically this means there is no string at all.

str[0] == '\0' means "the first character of str is character zero" (which marks the end of the string). This would mean there's a string, but it's empty. Think of an empty cup versus no cup at all; same idea.

In other languages you might write str == null vs str == "". They mean two different things. It's especially important to understand the difference in C, since trying to use a NULL pointer will crash the program.

Solution 7 - C

str == NULL it means that string having NO REFERENCE of string, because it's pointer is Null(means address of string is null).

str[0] == '\0'-- means string with 0 length.

Please let me know if any thing is wrong in this explanation or you still have the doubt.

Solution 8 - C

str == NULL 

means that str is not pointing to any adress = pointer is empty.

and str[0] == '\0' str is pointing to a valid adress and this line check if the first char (ie str[0]) is the digit 0 ( the ascii value of '\0') wich means the end of the string. then the string is empty. (there is no character in str : the first one is the end character)

Solution 9 - C

1 -> str == NULL determines if str is a NULL pointer 2 -> str[0] == '\0' determines if str is a 0 length c style string

so in this if ((str == NULL) || (str[0] == '\0')) shortcircuiting of OR operator come into picture as it ensures either string is not pointing to anything or to empty string..

Solution 10 - C

C# equivalent of it is:

if (string.IsNullOrEmpty(str))
{

}

Simple meaning is whether the string is NULL or an empty string.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjohnView Question on Stackoverflow
Solution 1 - CMysticialView Answer on Stackoverflow
Solution 2 - CShalom CraimerView Answer on Stackoverflow
Solution 3 - CJaredParView Answer on Stackoverflow
Solution 4 - CtriclosanView Answer on Stackoverflow
Solution 5 - CMatt JoinerView Answer on Stackoverflow
Solution 6 - CRenaView Answer on Stackoverflow
Solution 7 - CSanoj KashyapView Answer on Stackoverflow
Solution 8 - CHichamView Answer on Stackoverflow
Solution 9 - CAnshul gargView Answer on Stackoverflow
Solution 10 - CEstefany VelezView Answer on Stackoverflow