NULL Pointer in C Language

Satish Patil
1 min readJan 10, 2023

--

In the C programming language, a NULL pointer is a special type of pointer that does not point to any valid memory location. A NULL pointer is often used to represent the absence of a value or a null object.

In C, the NULL pointer is defined as a constant with the value 0. It is typically used in a comparison to check if a pointer is pointing to a valid memory location.

int *ptr = NULL;
if (ptr == NULL)
printf("ptr is a NULL pointer\n");

or

int *ptr = NULL;
if (!ptr)
printf("ptr is a NULL pointer\n");

Accessing memory through a NULL pointer can cause a segmentation fault, which is a common runtime error in C programs. This is why it’s important to always check if a pointer is NULL before dereferencing it.

if (ptr != NULL)
*ptr = 5; // it is safe to use

NULL pointers are also frequently used as sentinel values to indicate the end of a list or to indicate that a function did not return a value. They are also used as a default argument when a function requires a pointer parameter but it is not provided.

It’s important to keep in mind that in C, NULL pointers and void pointers are not the same thing. NULL pointer indicates that it doesn’t point to anything while void pointer is used to hold any type of data pointer but without any specific type information.

--

--