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 am using gcc for Linux.

+51
c gcc pointers cstring
Nov 30 '11 at 5:13
source share
10 answers

It is important to remember that str is not really a "string", but rather a pointer in the memory cell where the char (part of the string) is stored.

Next, we need to understand how the compiler sees all these elements. Let's look at their types:

  • str is of type char * (literally, "pointer to char ")
  • NULL - null pointer constant (at least in my system, ((void*)0) )
  • '\0' is a character constant (it really has an int type, but don't worry about it, it is usually used in a context that requires a char value)

See * in char * and void * ? This tells the compiler that these are pointer types (which is a fancy way of saying that variables of this type don't matter, they just point to it). Therefore, when the compiler sees char *str , it knows that you can ask to do something like *str or str[0] (they both do the same). We will come back to this 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 generated code goes to 0x0001 and gets the value. That way, if you do something like

 str + 1 

Then the compiler will generate code that looks something like this:

 fetch the value from where str is stored (0x0001) add 1 to that value 

What I'm sure you know. So, it should now be obvious what this line says:

 str == NULL 

Since NULL is the constant of a null pointer, this line checks if str null pointer (that is, a pointer that does not point to anything).

Therefore, the compiler usually generates code as follows:

 fetch the value from where str is stored check if that value is 0 

Remember, if you like, we told the compiler that str really a pointer type. Therefore, we are allowed to write this:

 *str 

And this 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, we get the value from the memory address 0x0200. Note that the compiler doesn't care if the string is actually stored there.

(I assume that you know that str[0] matches *str . This simplifies the explanation of what is happening.)

How about this?

 *str == '\0' 

So this line is valid:

 *str == (char) 0 

This causes the compiler to 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 

Summarizing:

  • The spelling str == NULL indicates whether the str pointer is specified .
  • Writing *str == '\0' indicates whether the str pointer points to an empty string (in fact, pointing to a memory location with zero).

(A "string" is by definition a "continuous sequence of characters ending with and including the first null character", so if the very first character of a string is '\0' , then the string is an empty string.)

+46
Nov 30 2018-11-11T00:
source share

str==NULL indicates whether the pointer is NULL.

str[0]=='\0' indicates whether the string has zero length.

In this code, the test:

 if ((str == NULL) || (str[0] == '\0')) 

used to detect when it is NULL or zero length.




Note that a short circuit plays a key role here: The test point is to make sure that str is a valid c-string with a length of at least 1.

  • The second test str[0] == '\0' will work only if str not NULL.
  • Therefore, the first test str == NULL needed to break earlier when str is NULL.
+108
Nov 30 '11 at 5:15
source share

Essentially

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

When you combine them, you check whether it is NULL or empty. This allows the function to eliminate both forms of empty data at the beginning of the method.

+27
Nov 30 '11 at 5:15
source share

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

str[0] == '\0' (if not a NULL pointer), checking the first element of str has a 0 value (a string without characters is only 0-completed)

+8
Nov 30 '11 at 5:17
source share

str==NULL indicates whether the string is NULL .

*str=='\0' indicates whether the string has zero length.

Note This answer is a Mystical 15 second answer game that had str=='\0' . Of course, the changes made in the first 3 or 4 minutes are not shown, and he recorded it ΰ² _ΰ² .

+7
Nov 30 '11 at 5:16
source share

str == NULL means "str points to the zero memory address" (or any other NULL address on your system). This usually means that the line is missing at all.

str [0] == '\ 0' means that "the first character str is a zero character" (which marks the end of a line). That would mean a string, but it is empty. Think of an empty bowl and a bowl; same idea.

In other languages, you can write str == null vs str == "" . They mean two different things. It is especially important to understand the difference in C, because trying to use the NULL pointer will cause the program to crash.

+6
Nov 30 2018-11-11T00:
source share

str == NULL this means that the string has NO REFERENCE of the string, because the pointer is Null (means the address of the string is null).

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

Please let me know if something is wrong with this explanation or you still have doubts.

+4
Nov 30 '11 at 12:50
source share
 str == NULL 

means that str does not point to any address = pointer is empty.

and str[0] == '\0' str points to a valid address, and this line checks to see if the first char (ie str [0]) is the number 0 (ascii value '\ 0'), which means the end String. then the line is empty. (there is no character in str: the first is the ending character)

+3
Nov 30 '11 at 13:03
source share

1 β†’ str == NULL determines whether str is a NULL pointer 2 β†’ str[0] == '\0' determines whether str is a style string 0 of length c

therefore, in this if ((str == NULL) || (str[0] == '\0')) shortcircuiting of the OR operator gets into the image, because it guarantees that no line points to anything or to the empty string.

+2
Dec 13 '11 at 13:19
source share

C # equivalent:

 if (string.IsNullOrEmpty(str)) { } 

The simple value is a NULL string or an empty string.

0
Dec 08 '11 at 11:01
source share



All Articles