A pointer that is not assigned any value but NULL is known as the NULL pointer. A Null Pointer is a pointer that does not point to any memory location. If you don't have any address to be specified in the pointer at the time of declaration, you can assign NULL value.
To define a null pointer assign the predefined constant NULL which is defined in standard(stddef) header files, including <stdio.h>
, <stdlib.h>
, and <string.h>
. Here, Null means that the pointer is referring to the 0th memory location. In the most libraries, the value of the pointer is 0 (zero).
#includeint *ip = NULL;
To test it for a null pointer before inspecting the value pointed to you might use code like below.
if(ip != NULL) printf("%d\n", *ip);
Following are the uses of a Null pointer:
We can also refer to the null pointer by using a constant 0, and you will see some code that sets null pointers by simply doing as beow
int *ip = 0;
As the definition of true in C is a value that is not equal to 0, you can tests for non-null pointers with abbreviated code like below.
if(ip) printf("%d\n", *ip);
It has the same meaning as our previous example; if(ip) is equivalent to if(ip != 0) and to if(ip != NULL).
int *ptr=(int *)0; float *ptr=(float *)0; char *ptr=(char *)0; double *ptr=(double *)0; char *ptr='\0'; int *ptr=NULL;
Let's look at the situations where we need to use the null pointer.
When we do not assign any memory address to the pointer variable.
#include <stdio.h> int main() { int *ptr; printf("Address: %d", ptr); // printing the value of ptr. printf("Value: %d", *ptr); // dereferencing the illegal pointer return 0; }
In the above code, we declare the pointer variable *ptr, but it does not contain the address of any variable. The dereferencing of the uninitialized pointer variable will show the compile-time error as it does not point any variable. According to the stack memory concept, the local variables of a function are stored in the stack, and if the variable does not contain any value, then it shows the garbage value. The above program shows some unpredictable results and causes the program to crash. Therefore, we can say that keeping an uninitialized pointer in a program can cause serious harm to the computer.
We can avoid the error created in the previous example by using the Null pointer. A null pointer is a pointer pointing to the 0th memory location, which is a reserved memory and cannot be dereferenced.
#include <stdio.h> int main() { int *ptr=NULL; if(ptr!=NULL) { printf("value of ptr is : %d",*ptr); } else { printf("Invalid pointer"); } return 0; }
In the above code, we create a pointer *ptr and assigns a NULL value to the pointer, which means that it does not point any variable. After creating a pointer variable, we add the condition in which we check whether the value of a pointer is null or not.
#include <stdio.h> int main() { int *ptr; ptr=(int*)malloc(4*sizeof(int)); if(ptr==NULL) { printf("Memory is not allocated"); } else { printf("Memory is allocated"); } return 0; }
In the above code, we use the library function, i.e., malloc()
. As we know, that malloc()
function allocates the memory; if malloc()
function is not able to allocate the memory, then it returns the NULL pointer. Therefore, it is necessary to add the condition which will check whether the value of a pointer is null or not, if the value of a pointer is not null means that the memory is allocated.