Search this blog...

What is NULL?

W hat is NULL? A friend of mine recently told me how this question had stumped his teacher in college and left her speechless for quite some time. Inspired by that, I posed myself the following question:
Q. In not more than 10 words,
Define NULL in the context of a programming language.

And i came up with the following few answers:
  1. Null is absolutely Nothing. Zilch.
  2. Null is something which is NOT defined.
  3. Null is the return-value of a function which has nothing to return.
  4. Null is the value of the pointer which points to nothing. ‡
  5. Null is the character at the end of a string.
  6. Null is the character with ASCII-code zero.
  7. Null is a byte with all its bits set to 0.
If you have good observation skills then you will notice that the list starts with a very general definition and then moves on to more specific ones.

In fact, [1] & [2] are so generic that they are applicable even in other fields (ex. Mathematics).

[3], [4], [5] & [6] are NOT definitions in the true sense. Rather they are commonly found implementations (or "Use-Cases") of NULL in programming languages.

Finally [7] gives us a one-line definition of NULL i.e. a byte with all its bits set to 0.

Now with reference to [7] we can see that-
  • Since ASCII-code zero is a byte with all bits set to 0, [6] is the exact same definition but just in so many other words.
  • [5] is NOT a definition of NULL. Rather it is a commonly found usage of NULL. One sees NULL terminated strings everywhere. It is so common that even programming-languages that do NOT have any explicit support for NULL, support lpStrZ i.e. null-terminated-strings.
  • [4] mentions yet another very common usage of NULL. Pointers are usually used to pass references to data between functions. As a special "use-case" whenever the "sender" function encounters a case wherein it has no (valid) data to pass, it assigns the value NULL to the pointer thereby signaling to the "receiver" function that the pointer reference is invalid.
  • [3] is yet another (not so common) usage of NULL. The following line
         //end of function
         return(0);
    }

    found at the end of so many common functions in any given C-code is the line that implements it. By convention, any function that completes execution successfully and has nothing to report (except for the fact that it succeeded) returns a NULL (i.e. zero in this case). In case of errors the functions usually return non-zero values which indicate the kind of error they encounter.
Thus, we can clearly see that there is nothing special about NULL. It is just another name for zero. The name NULL signifies the usage the value zero is put to i.e. to state that it points-to/stands-for/holds nothing.

Further reading:
  • boredzo.org/pointers
    " How a function pointer can even be the return value of a function...
    ...is really mind-bending, so stretch your brain a bit so as not to risk injury."
  • c-faq.com/null/index.html
    20 Qs. 20 straight answers.
    EVERYTHING you ever wondered about NULL pointers.
____________ _ _ ____________

If your function returns some data in some cases and no data in other cases, then define it as a non-void function and use NULL when you have nothing to return.

  Once a block of memory is freed, any pointers pointing to it must be set to NULL to prevent any accidental access.

BONUS:  Here is an even more intriguing question that the one i had started with:
Q. What is the data-type of NULL?
Hint: NULL is just yet another value like 13, 3.14 or 'c'.

2 comments :

  1. Hi CVS, I had a question for you based on the definition 3. What happens really when a function returns 0 (as in your case) while its return type is defined as void.

    Honestly NULL if really undefined or was nothing, then it should've been void in its truest sense. However, as you've rightly pointed out, in most use-cases, NULL is used with 0 as its definition (whether valid or not is another question).

    Now coming back, returning NULL from a function that shouldn't have returned anything... how valid is this?

    ReplyDelete
  2. I guess it depends upon the compiler. I have seen gcc compile the code with a simple warning "return with a value, in function returning void". This is because NULL is defined as void*(0).

    Semantically speaking, NULL and void are equivalent words. But syntactically speaking, void happens to indicate that there will be NO return statement.

    Any return statement in a function (even returning NULL) signifies that the function is non-void. Hence return(NULL) & void are contradictory.

    What it means to the programmer:
    If your function returns some data in some cases and no data in other cases, then define it as a non-void function and use NULL when you have nothing to return.

    If your function NEVER has any data to return, then better define it as void.

    ReplyDelete