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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.