Generic Pointers or Void Pointer in C Programming Language

Rumman Ansari   Software Engineer   2019-03-31   21125 Share
☰ Table of Contents

Table of Content:


Generic Pointers / Void pointer

When a variable is declared as being a pointer to type void, it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer.

This is very useful when you want a pointer to point to data of different types at different times.

Void pointer is a specific pointer type – void * – a pointer that points to some data location in storage, which doesn’t have any specific type. Void refers to the type. Basically the type of data that it points to is can be any. If we assign address of char data type to void pointer it will become char Pointer, if int data type then int pointer and so on. Any pointer type is convertible to a void pointer hence it can point to any value.

Why Void Pointers is important

  1. Suppose we have to declare integer pointer, character pointer and float pointer then we need to declare 3 pointer variables.
  2. Instead of declaring different types of pointer variable it is feasible to declare single pointer variable which can act as an integer pointer, character pointer.

Declaration of Void Pointer

void * pointer_name;

Void Pointer Example :

void *ptr;    // ptr is declared as Void pointer

char c;
int i;
float f;

ptr = &c;  // ptr has address of character data
ptr = &i;  // ptr has address of integer data
ptr = &f;  // ptr has address of float data

Explanation :

void *ptr;
  1. Void pointer declaration is shown above. 
  2. We have declared 3 variables of integer, character and float type.
  3. When we assign the address of the integer to the void pointer, the pointer will become Integer Pointer.
  4. When we assign the address of Character Data type to void pointer it will become Character Pointer.
  5. Similarly, we can assign the address of any data type to the void pointer.
  6. It is capable of storing the address of any data type

Example of Generic Pointer

Here is some code using a void pointer:


#include<stdlib.h>
 
int main()
{
    int x = 4;
    float y = 5.5;
     
    //A void pointer
    void *ptr;
    ptr = &x;
 
    // (int*)ptr - does type casting of void 
    // *((int*)ptr) dereferences the typecasted 
    // void pointer variable.
    printf("Integer variable is = %d", *( (int*) ptr) );
 
    // void pointer is now float
    ptr = &y; 
    printf("\nFloat variable is= %f", *( (float*) ptr) );
 
    return 0;
}

Another Example

#include"stdio.h"
int main()
{
  int i;
  char c;
  void *the_data;

  i = 6;
  c = 'a';

  the_data = &i;
  printf("the_data points to the integer value %d\n", *(int*) the_data);

  the_data = &c;
  printf("the_data now points to the character %c\n", *(char*) the_data);

  return 0;
}