NULL Pointer in C

NULL Pointer is a pointer that is pointing to nothing(i.e. not pointing to any valid object or memory location). In case, if we don’t have an address to be assigned to a pointer, then we can simply use NULL. NULL is used to represent that there is no valid memory address.

Syntax

datatype *ptrName = NULL;

Example

The below example demonstrates the value of the NULL pointer.

C




// C program to show the value of NULL pointer on printing
#include <stdio.h>
int main()
{
    // Null Pointer
    int* ptr = NULL;
 
    printf("The value of ptr is %p", ptr);
    return 0;
}


Output

The value of ptr is (nil)

Note NULL vs Uninitialized pointer – An uninitialized pointer stores an undefined value. A null pointer stores a defined value, but one that is defined by the environment to not be a valid address for any member or object.

NULL vs Void Pointer – Null pointer is a value, while void pointer is a type

Dangling, Void , Null and Wild Pointers in C

In C programming pointers are used to manipulate memory addresses, to store the address of some variable or memory location. But certain situations and characteristics related to pointers become challenging in terms of memory safety and program behavior these include Dangling (when pointing to deallocated memory), Void (pointing to some data location that doesn’t have any specific type), Null (absence of a valid address), and Wild (uninitialized) pointers.

Similar Reads

Dangling Pointer in C

A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. Such a situation can lead to unexpected behavior in the program and also serve as a source of bugs in C programs....

Void Pointer in C

...

NULL Pointer in C

...

Wild pointer in C

...