Generic Pointers (Void Pointers) in C: Understanding Usage

Rumman Ansari   Software Engineer   2024-07-05 05:54:07   20549  Share
Subject Syllabus DetailsSubject Details 1 Questions
☰ TContent
☰Fullscreen

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:

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

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

No Program Data.

Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.