Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 778
john Avatar asked Nov 30 '11 05:11

john


People also ask

What is the difference between null and 0 in C?

In computer programming, null is both a value and a pointer. Null is a built-in constant that has a value of zero. It is the same as the character 0 used to terminate strings in C. Null can also be the value of a pointer, which is the same as zero unless the CPU supports a special bit pattern for a null pointer.

What is str i 0 in C?

The part str[i] - '0' takes the ASCII character of the corresponding digit which are sequentially "0123456789" and subtracts the code for '0' from the current character. This leaves a number in the range 0..

Is 0 the same as null?

The answer to that is rather simple: a NULL means that there is no value, we're looking at a blank/empty cell, and 0 means the value itself is 0. Considering there is a difference between NULL and 0, the way Tableau treats these two values therefore is different as well.

What is a null character is 0 and null character same if not justify your answer with an example?

A null character is a character with all its bits set to zero. Therefore, it has a numeric value of zero and can be used to represent the end of a string of characters, such as a word or phrase.


2 Answers

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.
like image 191
Mysticial Avatar answered Oct 12 '22 14:10

Mysticial


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.)

like image 42
Shalom Craimer Avatar answered Oct 12 '22 13:10

Shalom Craimer