- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
NULL Pointer in C
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: %f\n", 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.