Updated March 28, 2023
Introduction to Null pointers in C
In C programming language, a variable that can point to or store the address of another variable is known as pointers. In C programming language pointers are used to point to the memory that is allocated dynamically or at run time and a pointer can be of any data type like int, float, char, etc. In this article, we are discussing the null pointer in C, where NULL is constant with value 0 in C. So the null pointer is defined as the pointer that is assigned to zero to make it null pointer or a pointer that does not store any valid memory address or an uninitialized pointer are known as a NULL pointer. In general, we can a pointer that does not point to any object is known as a null pointer.
How does Null pointer work in C?
A null pointer in C is a pointer that is assigned to zero or NULL where a variable that has no valid address. The null pointer usually does not point to anything. In C programming language NULL is a macro constant that is defined in a few of the header files like stdio.h, alloc.h, mem.h, stddef.h, stdlib.h. Also, note that NULL should be used only when we are dealing with pointers only. Simple syntax for declaring NULL pointer is as follows:
Syntax #1
int *pointer_var = NULL;
Or
Syntax #2
We can directly assign the pointer variable to 0 to make it null pointer.
int *pointer_var = 0
Examples to Implement Null pointer in C
Let us see an example of how null pointers are created.
Example #1
Code:
#include <stdio.h>
int main ()
{
int *ptr = NULL;
printf("The value of pointer assigned is : %x\n", ptr );
return 0;
}
Output:
Explanation: In the above code, we are initializing the variable “ptr” to 0 (zero) so when we print the pointer value which Null pointer.
Example #2
Suppose let us take another example where the pointer variables are not assigned to any memory address.
Code:
#include <stdio.h>
int main()
{
int *pointer_var;
printf("Address of the given pointer variable: %d", pointer_var);
printf("Value of pointer variable is : %d", * pointer_var);
return 0;
}
Output:
Explanation: In the above code, the pointer_var variable is not assigned to zero nor it stores any address of any variable in it, when the code is executed during the compile-time it gives an error where it throws garbage value which might harm your computer. So usually when we try to write or read from a null pointer we get run time error as we saw in the above code which we get segmentation fault which is a null pointer exception sometimes it also throws an exception as null pointer exception. In most of the examples, a null pointer is used to denote or indicate the end of the list.
Example #3
To avoid this exception we can rewrite the above code as
Code:
#include <stdio.h>
int main()
{
int * pointer_var =NULL;
if(pointer_var!=NULL)
{
printf("Value of pointer variable is : %d",* pointer_var);
}
else
{
printf("Invalid pointer");
}
return 0;
}
Output:
Explanation: In the above-modified code, we assign a pointer_var to the “NULL” value and we check with the condition if the value of the pointer is null or not. In most of the operating system, codes or programs are not allowed to access any memory which has its address as 0 because the memory with address zero 0is only reserved by the operating system as it has special importance, which states that the pointer is not intended to point to any memory location that can be accessible. So by default, we can say that if a pointer is assigned to zero then it is nothing but it only points to nothing.
So there is a way to check for the pointer is null or not by using if(ptr) results in 1 if the pointer is not null and if(!ptr) results in 1 when the pointer is null as we did in the above-modified program.
Example #4
Let us see the use of null pointers in C programming language as below:
Null pointers are used to avoid crashing down of the program: As we saw earlier if we declare any pointer without assigning anything to it then it takes garbage value where it may result in crashing of the system program. So to avoid such situations we use null pointers where variables are assigned or declared as NULL or zero which is known as a null pointer.
Code:
#include<stdio.h>
void func(int *ptrvarB)
{
if(ptrvarB == NULL)
{
//Handle NULL pointer input
printf("It is null pointer");
}
else
{
printf("It is not a null pointer");
}
}
void main()
{
int *ptrvarA = NULL;
func(ptrvarA);
}
Output:
Explanation: In the above code, we are defining function func() where we are passing a pointer ptrvarA and when the function func() is called it checks if the passed pointer is a null pointer or not. So we have to check if the passed value of the pointer is null or not because if it is not assigned to any value it will take the garbage value and it will terminate your program which will lead to the crashing of the program.
- Another use is when we are freeing up the memory locations: In a few cases we do not need the memory data anymore where we are again pointing to the same memory then we delete that data to free up the memory. But the pointer is still pointing to the same memory location even after deleting the data from that memory. Such pointers are called dangling pointers this also can be avoided by using null pointer by setting the dangling pointer to null.
Conclusion
In C programming language a Null pointer is a pointer which is a variable with the value assigned as zero or having an address pointing to nothing. So we use keyword NULL to assign a variable to be a null pointer in C it is predefined macro. And we should note that once the data is not in use the memory allocated to it must be freed else it will again lead to the dangling pointer. And also note that never declare any pointer without assigning NULL because the program when executes it throws an error during runtime.
Recommended Articles
This is a guide to Null pointer in C. Here we discuss how Null pointer work in C with syntax and examples to implement with proper codes and outputs. You can also go through our other related articles to learn more –