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:
- Null is absolutely Nothing. Zilch.
- Null is something which is NOT defined.
- Null is the return-value of a function which has nothing to return. †
- Null is the value of the pointer which points to nothing. ‡
- Null is the character at the end of a string.
- Null is the character with ASCII-code zero.
- Null is a byte with all its bits set to 0.
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.
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'.
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.
ReplyDeleteHonestly 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?
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).
ReplyDeleteSemantically 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.