”;
NULL Pointer in C
A NULL pointer in C is a pointer that doesn”t point to any of the memory locations. The NULL constant is defined in the header files stdio.h, stddef.h as well as stdlib.h.
A pointer is initialized to NULL to avoid the unpredicted behavior of a program or to prevent segmentation fault errors.
Declare and Initialize a NULL Pointer
This is how you would declare and initialize a NULL pointer −
type *ptr = NULL;
Or, you can use this syntax too −
type *ptr = 0;
Example of a NULL Pointer
The following example demonstrates how to declare and initialize a NULL pointer −
#include <stdio.h> int main() { int *p= NULL;//initialize the pointer as null. printf("The value of pointer is %u",p); return 0; }
Output
When you run this code, it will produce the following output −
The value of pointer is 0.
Applications of NULL Pointer
Following are some of the applications of a NULL pointer −
- To initialize a pointer variable when that pointer variable isn”t assigned any valid memory address yet.
- To pass a null pointer to a function argument when we don”t want to pass any valid memory address.
- To check for a null pointer before accessing any pointer variable so that we can perform error handling in pointer-related code. For example, dereference a pointer variable only if it”s not NULL.
A NULL pointer is always used to detect the endpoint of trees, linked lists, and other dynamic data structures.
Check Whether a Pointer is NULL
It is always recommended to check whether a pointer is NULL before dereferencing it to fetch the value of its target variable.
Example
Take a look at the following example −
#include <stdio.h> int main(){ int *ptr = NULL; // null pointer if (ptr == NULL) { printf("Pointer is a NULL pointer"); } else { printf("Value stored in the address referred by the pointer: %d", *ptr); } return 0; }
Output
When you run this code, it will produce the following output −
Pointer is a NULL pointer
Check Memory Allocation Using NULL Pointer
The malloc() and calloc() functions are used to dynamically allocate a block of memory. On success, these functions return the pointer to the allocated block; whereas on failure, they return NULL.
Example
The following example shows how you can use the NULL pointer to check whether memory allocation was successful or not −
#include <stdio.h> #include <stdlib.h> int main(){ int* ptr = (int*)malloc(sizeof(int)); if (ptr == NULL){ printf("Memory Allocation Failed"); exit(0); } else{ printf("Memory Allocated successfully"); } return 0; }
Output
Run the code and check its output −
Memory Allocated successfully
NULL File Pointer
Checking if the FILE pointer returned by the fopen() function is NULL is always a recommended approach to avoid runtime errors in file-related processing.
Example
The following example shows how you can use the NULL file pointer to ensure that a file is accessible or not −
#include <stdio.h> #include <string.h> int main(){ FILE *fp; char *s; int i, a; float p; fp = fopen ("file3.txt", "r"); if (fp == NULL){ puts ("Cannot open file"); return 0; } while (fscanf(fp, "%d %f %s", &a, &p, s) != EOF) printf ("Name: %s Age: %d Percent: %fn", s, a, p); fclose(fp); return 0; }
You should always initialize a pointer variable to NULL when the target variable hasn”t been assigned any valid memory address yet.
”;