What is a NULL pointer in C?

A NULL pointer does not point to any valid memory. We set a pointer to NULL when we have no address to give yet, and check it before using.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = NULL;

    if (p == NULL) {
        printf("No memory yet\n");
    }
    return 0;
}

Output:

Output of the above program is

No memory yet

Reading or writing through a NULL pointer crashes the program. Functions like malloc() and fopen() return NULL when they fail.


Learn more about the similar topics:
Tutorials
C Pointers
C Pointer Arithmetic
C Pointer to Pointer
C Pointers and Arrays
C Pointers and Functions
C Dynamic Memory Allocation
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is a pointer in C?
What is a NULL pointer in C?
What is the difference between malloc() and calloc() in C?
What is a memory leak in C and how to avoid it?
What is a dangling pointer in C?
What is the difference between an array and a pointer in C?
What is a void pointer in C?