Null Pointer - C Pointer

C examples for Pointer:Introduction

Introduction

A pointer should be assigned to zero if it does not represent a valid address.

A pointer with zero value is called a null pointer.

We can check if a pointer is zero.

Demo Code

#include <stdio.h>
int main(void) {
    int i = 10;// w  w w .j  a  v a  2 s .c om
    int* p; /* pointer to an integer */

    p = &i;  /* address of i assigned to p */

    if (p != 0) { /* check for null pointer */
       *p = 10;
    }
}

The constant NULL can be used to signify a null pointer.

NULL is typically defined as zero in C.

The constant is defined by several standard library files, including stdio.h and stddef.h.

Demo Code

#include <stdio.h>
int main(void) {
    int i = 10;/* w ww. j av  a2 s  .c  o  m*/
    int* p; /* pointer to an integer */

    p = &i;  /* address of i assigned to p */

    if (p != NULL) {
       *p = 10;
    }
}

Related Tutorials