Monday, April 6, 2015

Void Pointers

Declaration of a void pointer is given below:

void *ptr1;

ptr1 is a void pointer. ptr1 is a pointer that can point to anything. Void pointers are also known as generic pointers.

Pointers defined using specific data type cannot hold the address of the some other type of variable i.e., for example

float *f;
int i;
f = &i;

In the above statement f = &i produces a compilation error because f is a pointer which can point only to a float variable. The above problem can be solved by general purpose pointer called void pointer.

void *v;
int i;
v = &i; //this is valid

No comments:

Post a Comment