Author:ys2310
2008年春にNew York Cityにあるふる〜い大学を卒業。
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:
#include <stdio.h>
int main()
{
int num[3] = {10,20,30};
char name[30] = "Welcome to C World";
int *pint = NULL;
void *pvoid = NULL;
int i;
pint = #
for ( i=0; i<3; i++ )
printf("%d ",*(pint + i ));
printf("n");
// The same can be done using void pointer as follows.
pvoid = #
for ( i=0; i<3; i++ )
printf("%d ",*((int *)pvoid + i ));
// Same void pointer can be cast to char.
printf("n");
pvoid = name;
for( i=0; i < strlen( name); i++ )
printf("%c", *((char *) pvoid + i));
getch();
}