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' holds 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.


A program which prints the value of NUll and NULL pointer.


#include <stdio.h>


int main (  )

{

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



    printf ("value of NULL = %d\n\n", NULL);

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

}


Output

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;
}


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;
}

Output

When we will compile 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

#include<stdio.h>

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


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

p=&x;
printf ("%f\n",*((float*)p));

p=&y;
printf ("%c\n",*((char*)p));

}


Output

41
49.500000
a


Recommended Posts



No comments:

Post a Comment