A pointer that doesn’t refer to any address of value but NULL is known as a NULL pointer. When we assign a ‘0’ value to a pointer of any type, then it becomes a Null pointer.
In C, a NULL pointer is a pointer that does not point to any memory location. It is a special constant defined in the standard library (usually as (void *)0
) that is used to indicate that the pointer does not refer to a valid object.
Here’s how you can define and use a NULL pointer:
int *ptr = NULL;
In this example, ptr
is a pointer to an integer that has been initialized to NULL. This means ptr
currently doesn’t point to any valid memory location.
It’s important to note that dereferencing a NULL pointer (i.e., trying to access the value it points to) leads to undefined behavior, which could result in program crashes or unexpected results. Therefore, it’s crucial to always check whether a pointer is NULL before attempting to dereference it.