c
  1. c-null-pointer

C Null Pointer

Syntax

dataType *pointer = NULL;

Example

#include <stdio.h>

int main() {
    int *ptr = NULL;

    printf("The value of ptr is: %p", ptr);
    
    return 0;
}

Output

The value of ptr is: 0x0

Explanation

A null pointer is a pointer that does not point to any memory location. In C, a null pointer is represented by the value NULL. When a pointer is initialized to NULL, it means that it is not pointing to any memory location.

Use

A null pointer is used to represent the absence of a valid memory address. It can also be used as a flag to indicate that a pointer is not pointing to a valid memory location. This can be useful in situations where you need to differentiate between a valid and an invalid pointer.

Important Points

  • A null pointer is a pointer that does not point to a valid memory address.
  • In C, a null pointer is represented by the value NULL.
  • It is important to check whether a pointer is null before dereferencing it to avoid segmentation faults and undefined behavior.
  • A null pointer can be assigned any pointer type, not just the pointer to void.
  • You can use the null pointer to terminate linked lists and other data structures that use pointers.

Summary

In C, a null pointer is a pointer that does not point to a valid memory address. It is represented by the value NULL. Null pointers are used to represent the absence of a valid memory address and can be used as a flag to indicate that a pointer is not pointing to a valid location. It is important to check whether a pointer is null before dereferencing it to avoid segmentation faults and undefined behavior.

Published on: