Void pointer or generic pointer is a special type of pointer that can be pointed at objects of any data type. A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type.
Pointers defined using specific data type cannot hold the address of the some other type of variable i.e., it is incorrect in C++ to assign the address of an integer variable to a pointer of type float.
Example:
float *f; //pointer of type float int i; //integer variable f = &i; //compilation error |
The above problem can be solved by general purpose pointer called void pointer.
Void pointer can be declared as follows:
void *v // defines a pointer of type void |
The pointer defined in this manner do not have any type associated with them and can hold the address of any type of variable.
Example:
void *v; int *i; int ivar; char chvar; float fvar; v = &ivar; // valid v = &chvar; //valid v = &fvar; // valid i = &ivar; //valid i = &chvar; //invalid i = &fvar; //invalid |
nice material. thank you very much.
Good material are produce by ur website. plz give us to better and better materials. thanks a lot
Clearly understood the concept. Thank you.
explanation given in very simple language.awesome description.thnx
very well expaltion. thanks.
frends jahan tak maine is topic ko samjha hai wo ye hai ki generic pointer is also known as void pointer as we know ki har pointer ka ek data type hota hai but yahan hum koi specific data type ke saath pointer declare nai karte balki void is keyword which use in as data type at the time of generic/void pointer concept…..ye pointer vo hai jiski value yani uska data type kabhi bhi hum typecast karsakte hain by using
Ex: float *float_pointer;
int *int_pointer;
void *void_pointer;
. . . . . . . .
. . . . . . . .
void_pointer = float_pointer;
. . . . . . . .
. . . . . . . .
void_pointer = int_pointer;
A void pointer is generally used as function parameters, when the parameter or return type is unknown.
#include
void main()
{
int i=5;
do
printf(“in do”);
while(i<4);
}
nice training
aspecial type of pointer that can be pointed at objects of any data type