Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use NULL and when to use '\0' in linked list in C?

I learned that in C: null char == '\0' == NULL, and I wrote a loop below to read from the start to the end of a char[] in C.

// case #1
char buf[32];
while (buf[i] != NULL){
    //do something...
}  

However, my gcc compiler gave me a warning: comparison between pointer and integer. Someone mentioned that I was confusing two separate concepts: NULL is for pointers, whereas '\0' is for characters. So to get rid of the warning, I should use '\0' since my loop tests a char.

Now I am writing a linked list, and testing if a head pointer points to a node or not. Since it's struct, it's reasonable to use if (h1 == NULL) but apparently the compiler also compiles when I use if (h1 == '\0') even though the node is a struct but not a char. Can someone give some help why both '\0' and NULL can be used in this case while they can't be both use on the first case?

// case #2
struct ListNode {
    int val;
    struct ListNode *next;
};
like image 972
LED Fantom Avatar asked Sep 20 '15 19:09

LED Fantom


People also ask

What is the difference between assigning 0 and null?

'\0' is defined to be a null character. It is a character with all bits set to zero. This has nothing to do with pointers. '\0' is (like all character literals) an integer constant with the value zero.

What does null mean in a linked list?

The null reference occurs in the link part of the final node of a linked list. A program that maintains a head and a tail reference may set these references to null, which indicates that the list is empty (has no nodes).

IS null always defined as 0?

The null pointer constant is always 0. The NULL macro may be defined by the implementation as a naked 0 , or a cast expression like (void *) 0 , or some other zero-valued integer expression (hence the "implementation defined" language in the standard). The null pointer value may be something other than 0.

What is the purpose of null character in C?

Today the character has much more significance in the programming language C and its derivatives and in many data formats, where it serves as a reserved character used to signify the end of a string, often called a null-terminated string.


1 Answers

This is a very common confusion. A "null character" (often spelled NUL, with only one L, for historical reasons) is a character that compares equal to zero. A "null pointer" is a pointer that compares equal to zero. The difference is only the type -- but that is an important difference. The compiler is allowed, but not required, to object to your program, because you are comparing a character value to a null pointer constant.

C has a fairly loose type system, especially when it comes to integer constants, so you can get away with using constants with a formally incorrect type a lot of the time. This is especially true when it comes to zero: the integer literal 0 can be used everywhere it is safe to use the macro NULL or the character literal '\0'. In fact, the implementation is specifically allowed to use #define NULL 0 in all editions of the C standard. Because of this, there are a small handful of contexts where one absolutely must use (T*)0 for some concrete type T, instead of either bare 0 or NULL, such as when passing null pointers to a function that takes a variable number of arguments (e.g. execl).

As further icing on the confusion cake, in C character literals have type int, not char, and '\0' is a legitimate null pointer constant. (In C++ neither of those things is true.)

Best practice in C, IMNSHO, is to use '\0' for the null character, and 0not NULL — for the null pointer.

like image 91
zwol Avatar answered Oct 19 '22 22:10

zwol