a value indicating that a pointer does not refer to a valid object
Videos
Any pointer type with the value 0 is called a null pointer. Here is the explanation from the C standard ยง6.3.2.3:
- An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function
Null pointer is a pointer that points to memory address 0. Most system reserve this address so that no object will ever reside in there. This reservation allows to use it as blank address.
If you try to read or write from null pointer you will get runtime error which is sometimes called segmentation fault, or null pointer exception.
In your example the null pointer is used to indicate end of the list. So this condition (struct entry *) 0, checks if you have reached end of the list and iteration should stop
Usually it considered better form to use constant NULL instead of literal value 0. This makes code more readable, and also covers very rare case when NULL pointer is not a 0
The cast (struct entry *) is just to avoid compiler warning, because literal 0 is of type integer not a pointer. That's another reason to use constant NULL, because it is usually defined as (void*) 0 which compares nicely to any pointer value without a warning from compiler
If we want to "Nullify" something why dont we create a pointer in a controlled way that points to value 0?
Hello all again,
I have another stupid question here lol, so I'm trying to wrap my head around NULL. Im currently under the impression that NULL is a built in constant that has a value of zero, but what does that actually mean? When would it be appropriate to use null? If someone could explain it in layman's terms that would be super helpful!