The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Void Pointer

What Is Void Pointer?

C programming doesn't provide any generic pointer to pointer type. Instead it provides generic pointer type called void pointer. Thus void pointer is most commonly called as general purpose pointer.

Why Void Pointer Alone Generic?

Void pointer is alone generic because void pointer does not have any associated data type with it. Being generic void pointer can store address of a variable irrespective of it's datatype.

Void Pointer Purpose

The purpose of void pointer is awesome, let us consider that we have to declare three pointer variables of three different type say char*, int* and float* if we have no idea about void pointer we definitely going to declare three pointer variable with particular datatypes. Now your code looks like superfluous with datatypes, to overcome this drawbacks void* was introduced. when you declare a pointer variable of type void a conversions to a particular datatypes are applied automatically by the compiler i.e) if the value of a address stored in a void pointer is int datatype then void will convert into int type automatically at the time of compilation. If typecasting is not made, the compiler won't have any idea about what datatype the address in the void pointer points to.

void pointer in c

Syntax - Void Pointer

void-pointer
void *variable_name;

Void Pointer Dereferencing

Dereferencing pointer variable of void type is bit complex than usual pointer variable. Consider the following program which will not compile .

C Program - Void Pointer - Incorrect

void-pointer-incrct.c
#include <stdio.h>
int main()
{
int i = 5;
void *vptr; // void pointer declaration
vptr = &i;
printf("\nValue of iptr = %d ", *vptr);
return 0;
}

C Program - Void Pointer Program - Correct

While Dereferencing a pointer variable of void type typecasting is necessary. Consider the following program which will compile.

void-pointer-crct.c
#include <stdio.h>
int main()
{
int i = 5;
void *vptr; // void pointer declaration
vptr = &i;
printf("\nValue of iptr = %d ", *(int *)vptr);
return 0;
}
  • Value of iptr = 5

Note:

The void pointer "vptr" is initialized by the address of integer variable 'i'. So, the value must be referred using typecast. If you try to access the data without typecasting, the compiler will show an error.

C Program - Void Pointer To String

void-pointer-string.c
#include <stdio.h>
int function(); // function declaration
int main()
{
char *ptr = "void pointer to string";
void *vptr; // void pointer declaration
vptr = &ptr;
printf("%s" , *(char **)vptr);
return 0;
}
  • void pointer to string

Note:

In the above program pointer to pointer typecasting *(char **)vptr is done to retrieve the string stored in a character pointer variable using void pointer.

Interesting Facts About Void Pointer

  • If we assign a address of integer variable to void pointer. Then void pointer will convert into integer pointer by itself at the time of execution because typecasting becomes mandatory when we use void pointer.
  • If we assign a address of character variable to void pointer. Then void pointer will convert into character pointer by itself at the time of execution because typecasting becomes mandatory when we use void pointer.
  • If we assign a address of floating point variable to void pointer. Then void pointer will convert into floating point pointer by itself at the time of execution because typecasting becomes mandatory when we use void pointer.

Void Pointer Is Used

  • Void pointer is highly prefered in dynamic memory allocation using malloc() and calloc().
  • A void pointer is a most convention way in c for storing a raw address.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report