5.5. Generic Pointers
When a variable is declared as being a pointer to type
void it is known as a generic
pointer. Since you cannot have a variable of type
void, the pointer will not point to any data and
therefore cannot be dereferenced. It is still a pointer though, to
use it you just have to cast it to another kind of pointer first.
Hence the term Generic pointer.
This is very useful when you want a pointer to point to data
of different types at different times.
Here is some code using a void pointer:
Example 5-3. generic_pointer.c
i = 6;
c = 'a';
the_data = &i;
printf("the_data points to the integer value %d\n", *(int*) the_data);
the_data = &c;
printf("the_data now points to the character %c\n", *(char*) the_data);