The Concept of Null

In the realm of mighty C

Hi Reader.

I am feeling great today. So I thought, why can't I write something that is short, crisp yet useful? Yep, here is that small term we frequently use (at least in the System Programmer's head) called as - Null, pronounced as \ ˈnəl \.

As I have searched in Google for Null, in Merriam-Webster, it has so many definitions like these. That's great. But Are we here to discuss English language?

Definitely Not [like M.S Dhoni said so].

Alright, I will make no fuss. Welcome Again!

The concept of Null is interesting and sometimes misunderstood. for a newbie like me, confusion can occur because often we come across several similar, yet distinct concepts.

Here are the different \ 'nəl \s :

  • The null concept
  • The null pointer constant
  • The NULL macro
  • The ASCII NUL
  • A null string
  • The null statement

That's all. Told you, the list is a bit big but It's not exhaustive, isn't it? Let's go further to understand these.

When NULL is assigned to a pointer, it means the pointer does not point to anything. The null concept refers to the idea that a pointer can hold a special value that is not equal to another pointer. It does not point to any area of memory.

A null pointer and an uninitialized pointer are different. An uninitialized pointer can contain any value, whereas a pointer containing NULL does not reference any location in memory.

Two null pointers will always be equal to each other. There can be a null pointer type for each pointer type, such as a pointer to a character or a pointer to an integer, although this is uncommon.

The null concept is an abstraction supported by the null pointer constant. This constant may or may not be a constant zero.

C programmer need not be concerned with their actual internal representation.

The NULL macro is a constant integer zero cast to a pointer to void. In many libraries, it is defined as follows: #define NULL ((void )0)*

This is what we typically think of as a null pointer. Its definition frequently can be found within several different header files, including stddef.h, stdlib.h, and stdio.h.

If a nonzero bit pattern is used by the compiler to represent null, then it is the compiler’s responsibility to ensure all uses of NULL or 0 in a pointer context are treated as null pointers. The actual internal representation of null is implementation-defined. The use of NULL and 0 are language-level symbols that represent a null pointer.

The ASCII NUL is defined as a byte containing all zeros. However, this is not the same as a null pointer.

A string in C is represented as a sequence of characters terminated by a zero value. The null string is an empty string and does not contain any characters.

Finally, the null statement consists of a statement with a single semicolon.

A null pointer is a very useful feature for many data structure implementations, such as a linked list where it is often used to mark the end of the list.

If the intent was to assign the null value to pi , we use the NULL type as follows:

pi = NULL;

Interestingly, we can assign a zero to a pointer, but we cannot assign any other integer value. Consider the following assignment operations:

codeSnippet0.png A pointer can be used as the sole operand of a logical expression. For example, we can test to see whether the pointer is set to NULL using the following sequence:

codeSnippet1.png

Either of the two following expressions are valid but are redundant. It may be clearer, but explicit comparison to NULL is not necessary.

If pi has been assigned a NULL value in this context, then it will be interpreted as the binary zero. Since this represents false in C, the else clause will be executed if pi contains NULL .

codeSnippet2.png

A null pointer should never be dereferenced because it does not contain a valid address. When executed it will result in the program terminating.

To NULL or not to NULL ?

Either is perfectly acceptable; the choice is one of preference. Some developers prefer to use NULL because it is a reminder that we are working with pointers. Others feel this is unnecessary because the zero is simply hidden.

However, NULL should not be used in contexts other than pointers. It might work some of the time, but it is not intended to be used this way. It can definitely be a problem when used in place of the ASCII NUL character. This character is not defined in any standard C header file. It is equivalent to the character literal, '\0' , which evaluates to the decimal value zero.

The meaning of zero changes depending on its context. It might mean the integer zero in some contexts, and it might mean a null pointer in a different context.

Example:

codeSnippet3.png

We are accustomed to overloaded operators, such as the asterisk used to declare a pointer, to dereference a pointer, or to multiply. The zero is also overloaded. We may find this discomforting because we are not used to overloading operands.

That's it for now.

Feel free to reach out to me in case of any kind of feedback or suggestions to improve my writing.

Reference

[Textbook] Understanding and Using C Pointers by Richard M Reese

Keep Learning...

Bhanuprakash Eagala