I am reading “The practice of programming” and it says that a standard loop for walking along a list in C is:
for (p=list;p != NULL;p=p->next)
...
My question is how does NULL work? I myself have used this in programs, but never paused to ponder over how it works.
I suppose, NULL must be a standard macro defined in a very basic header file like stdio.h
but what it is defined as? Is it?
#define MACRO 0
here how does 0 work when we compare a pointer to 0. This 0 could be a valid memory address 0, then how do we distinguish to say that we actually mean invalid address i.e. the pointer has a logical empty value?
Thanks,
It’s defined as 0, yes. The thing is that 0 is not a valid memory address; computers are actually put together such that trying to access that address is an error.
Try running this program on your machine, if you want to insist that 0 is a legal address: