Introduction To Void Pointer
We all know that the address assigned to a pointer should be the same data type as it is specified in the declaration of that pointer. For example, if we declared a pointer with integ
er (int) data type, then it should point to the address of any variable having the same integer type. We can not use the integer pointer (int *Ptr) to point to an address of a variable with float type variable or anything else except integer type. We can use the void pointer in C to do this.To overcome this typecasting problem in the pointer, we use a pointer to void. It is also called a generic pointer. The void pointer can point to any data type. So that we can assign this void pointer to any data type. It is also can be assigned to any type of pointer without performing any explicit typecasting.
The Void Pointer in C is a special type of pointer that can assign it to any type of data pointer. Simply you can say that if you don’t know the data type of any data that is you are going to assign it to a pointer, then you can use a void pointer. The compiler has no idea what type of object a void pointer is really pointing to.
Syntax Of Void Pointer
//Void pointer syntax
void *pointer name;
Void Pointer Declaration
void *ptr;
Void Pointer In C Language
It is nothing like only in C we are using a void pointer. We are also using it in C++ or you can say in core Embedded C. It will be helpful for a programmer who is not sure about the data type of data given by the end-user. I hope you go through this tutorial, you understand well in deep. So let us take an example of a C program snippet for how to use it.
void func(void *Vptr, int piest)
{
if(piest==1)
printf("%d",*(int*)Vptr); // Typecasting for Integer pointer.
else if(piest==2)
printf("%c",*(char*)Vptr); // Typecasting for character pointer.
else if(piest==3)
printf("%f",*(float*)Vptr); // Typecasting for float pointer.
}
Why We Use Void Pointer
The main purpose of the use of a void pointer is the reusability feature. The Void pointers can store the object of any data-type, and also we can retrieve the object of any data-type by using the indirection operator with proper typecasting. Let’s go to understand how to use a void pointer in c example:
#include<stdio.h>
int main()
{
int iPtr=95; // initialization of iPtr integer variable 'iPtr'.
float fPtr=9.5; // initialization of iPtr float variable 'fPtr'.
char cPtr='P'; // initialization of iPtr char variable 'cPtr'.
void *ptr; // declaration of void pointer.
// assigning the address of variable 'iPtr' to void pointer.
ptr=&iPtr;
printf("value of 'iPtr' is : %d",*((int*)ptr));
// assigning the address of variable 'fPtr' to void pointer.
ptr=&fPtr;
printf("\nvalue of 'fPtr' is : %f",*((float*)ptr));
// assigning the address of variable 'cPtr' to void pointer.
ptr=&cPtr;
printf("\nvalue of 'cPtr' is : %cPtr",*((char*)ptr));
return 0;
}
//Output of the above program is-
value of iPtr is : 95
value of fPtr is : 9.500000
value of cPtr is : P
Advantages Of Void Pointer In C
- The void pointer is a special type of general pointer in C.
- It does not have any data type that is associated with it.
- It can store the address of any type of variable.
- The malloc() and calloc() functions in C programming returns void * or generic pointers.
- It also helps to implement any generic functions in C or C++ or Embedded C.
Size Of Void Pointer
The size of the void pointer in C is the same as the character pointer. Basically, as per the C programming point of view, the representation of a void pointer is equivalent to a character pointer.
To understand the concept of the void pointer, how external typecasting for the pointer is used for reading 4 Bytes,2 Bytes, 1 Byte data in embedded C.
#include<studio.h>
int main()
{
Void *ptr; //initialization of void pointer
int a = 0x15256456; // storing 32 bit Hexadecimal data in variable 'a'
ptr = &a; // store the address of a var pin void pointer
/*printing address of a var*/
printf("Address of a = %u\n",&a);
/*printing only 1 Byte of Hex data from a var*/
printf("Value of a = %x\n",*(char*) ptr);
/*printing only 2 Bytes of Hex data from a var*/
printf("Value of a = %x\n",*(short*) ptr);
/*printing only 4 Bytes of Hex data from a var*/
printf("Value of a = %x\n",*(int*) ptr);
return 0;
}
DEBUG CONSOLE TERMINAL
PS D: \PiEmbSysTech\PiEST_Linux\Test_Space> ./a.exe
Address of a = 6422296
Value of a = 56
Value of a = 6456
Value of a = 15256456
Limitations Of Void Pointer
We all know that really the void pointer helps in lot of difficulty programming where run time we need to change the pointer type. But still we have some limitations in using of void pointer. So that before we use it as a void pointer, we should know the disadvantages of it. Let us discuss it.
Void Pointer As Arithmetic Operation
The Void Pointer can not be used for arithmetic operations due to its concrete size. But in the GNU C library, it is allowed by considering the size of the void is 1. For example, the following program compiles and runs fine in the GCC compiler.
int main()
{
int array1[2] = {1, 2};
void *ptr = &array1;
ptr = ptr + sizeof(int);
printf("%d", *(int *)ptr);
return 0;
}
//Output
2
Void Pointer As Indirection Operator (*)
The void pointer can not be dereferenced. Basically, by using the indirection operator (*) in c we can get back the value which is pointed by the pointer. But in the case of a void pointer, we have a limitation in using it. So we cannot use the indirection operator directly. Because a void pointer is not having any data type, so that it creates a problem for the compiler to predict the size of the pointed object at compilation time. So before dereferencing the void pointer, we have to typecast it. It helps the compiler to predict the data types.
int main()
{
int piest = 10;
void *ptr = &piest;
printf("%d", *ptr);
return 0;
}
//Output
Compiler Error: 'void *' is not a pointer-to-object type
If you want to use it then you need to change the program like below:
int main()
{
int piest = 35;
void *ptr = &piest;
printf("%d", *(int *)ptr);
return 0;
}
//Output
35
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.
One of the best explanation for Void Pointer
Very good explanation on the void pointer.