Google News
logo
Embedded C - Interview Questions
What is Void Pointer in Embedded C and why is it used?
Void pointers are those pointers that point to a variable of any type. It is a generic pointer as it is not dependent on any of the inbuilt or user-defined data types while referencing. During dereferencing of the pointer, we require the correct data type to which the data needs to be dereferenced.
 
For Example :
int num1 = 20;    //variable of int datatype 
void *ptr;        //Void Pointer
*ptr = &num1;    //Point the pointer to int data
print("%d",(*(int*)ptr));    //Dereferencing requires specific data type 

char c = 'a';
*ptr = &c;    //Same void pointer can be used to point to data of different type -> reusability
print("%c",(*(char*)ptr));
 
Void pointers are used mainly because of their nature of re-usability. It is reusable because any type of data can be stored.
Advertisement