Thursday, 24 January 2019

Types of pointers in C (Null pointer, wild pointer, void pointer, dangling pointer, double pointer)

Pointers in C are used to the defined the address of another variable of the same data type. For example

int q = 10; //declaration and initialization of variable

Types of pointers in C

int *p;       // declaration of pointer variable

Types of pointers in C

p = &q    // initialization of pointer variable 'p'

types of pointers in C

Pointer variable 'p' keep the address of 'q' that is 2000. To access the value of the variable 'q', use *p (asterisk p).  For detailed knowledge: Pointers in C

Types of pointers in C

NULL pointer

  • If we do not want to assign the address of another variable to pointer variable then we use a NULL pointer.
  • A NULL pointer is a predefined macro and its value is 0. 
  • In int *p = NULL; statement, p is an integer type pointer variable which is initialized with NULL and known as a NULL pointer.


#include <stdio.h>
int main (  )


    int *p = NULL; //declaration and initialization of NULL pointer

    printf ("value of NULL pointer = %u", p);

    return 0;


value of null = 0
value of NULL pointer = 0

Wild pointer

  • Those pointer variables which are declared but not initialized with any address is known as a wild pointer. These pointer variables contain garbage value. 
  • Difference between null pointer and the wild pointer is that null pointer is initialized with zero whereas, a wild pointer is uninitialized and consist garbage value. 

For example

#include <stdio.h>
int main ( )
    int *a; //wild pointer
    int b = 10;
    // After initialization pointer is not a valid pointer
    a = &b; 
    return 0;

A void pointer in C

  • A void pointer is an untyped pointer i.e. those pointers which are the point to any variable of any data type. 
  • A void pointer is also known as the generic pointer and general purpose pointer because it can store the address of any type of data. 
  • Declaration of void pointer is similar to normal pointer only difference is that void keyword is instead of data type (int, float, char). for example Void *p;

  • Dereferencing a void pointer in C
Accessing the value of that address which is stored in the pointer variable is called dereferencing. We need a type-casting because the void pointer is not associated with any data type. 

#include <stdio.h>
int main ( )
    void *p;
    int q =10;
    p = &q;
    printf ("*p =%d\n", *p);
    return 0;


When we will compile the above code then we will get an error because in the above tried to dereference the void pointer without type-casting.

Type-cast  the void pointer


void main ( )
int w=41;
float x=49.5;
char y='a';
void *p;

// *((int*)p)) is type-casting of pointer variables
printf ("Integer variable is %d\n",*((int*)p));

printf ("Float variable is %f\n",*((float*)p));

printf ("Character variable is %c\n",*((char*)p));




What is near, far and huge pointers in C?

Near, far and huge pointers are designed for MS-DOS. These pointers are used only on the 16-bit machine, so, nowadays, they are not used.

Near pointers in C is used to store 16-bit address on the data segment, code segment or stack segment, only on the 16-bit machine. At a time, it can only access 64Kb data.

Far pointers in C is used to store 32-bit data and can access the memory outside the current segment. To use this, the compiler allocates a segment register to store segment address, then another register to store offset within the current segment.

Huge pointers in C are of 32-bit and can access the memory outside this segment. In far pointer, a segment is fixed but in the huge pointer, we can increase or decrease the segment portion.

Recommended Posts

No comments:

Post a Comment