Null Pointers in C Programming

We have seen above that it is not necessary to allocate memory as soon as we declare it. We can assign memory anywhere in the program but before actually using it in the program. But what will be pointer pointing to till we allocate memory to it? Some memory location is the system which may or may not be valid. Sometimes we might not know what address needs to be assigned to it. In these cases we cannot leave the pointer without allocated to any memory. It is always best practice to assign some memory to it. Hence we allocate NULL to a pointer indicating it is not pointing to any memory location. Now the pointer will not point to any invalid addresses or any address that are used by other programs / variables/ pointers. This kind of pointer is called null pointers.

int *intPtr = NULL;

In some systems, NULL indicates zero and hence it infers pointers are pointing to memory address ‘0’. But this address is not allowed to use by any programs as this memory address is allocated for operating system. But when a pointer is a null pointer, it always signals the compiler that it is not pointing to any variable or memory, rather than indicating that it is pointing to memory address ‘0’.

Translate »