void指针用于引用内存中的任何地址类型,其声明如下:
void *ptr;
以下程序对三种不同的数据类型使用同一指针:
int x = 33;
float y = 12.4;
char c = 'a';
void *ptr;
ptr = &x;
printf("void ptr指针 %d\n", *((int *)ptr));
ptr = &y;
printf("void ptr指针 to %f\n", *((float *)ptr));
ptr = &c;
printf("void ptr指针 to %c", *((char *)ptr));
在使用对空指针的引用时,必须先将指针类型转换为适当的数据类型,然后再通过*的引用。
---