void *从本质上讲是一种指针的类型,就像 (char *)、(int *)类型一样.但是其又具有
特殊性,它可以存放其他任何类型的指针类型:例如:
char *array="I am the pointer of string";
void * temp; //temp可以存放其他任何类型的指针(地址)
temp=array; // temp 的指针类型
cout<<array<<endl;
cout<<temp<<endl;
cout<<(char *)temp<<endl;
运行结果:
I am the pointer of string
0x0042510C (这个值就是array指针变量所存储的值)
I am the pointer of string
2.但是不能将void *类型的值赋给其他既定的类型 ,除非 经过显示转换:
例如:
int a=20;
int * pr=&a;
void *p;
pr=p //error,不能将空的类型赋给int *
void *从本质上讲是一种指针的类型,就像 (char *)、(int *)类型一样.但是其又具有
特殊性,它可以存放其他任何类型的指针类型:例如:
char *array="I am the pointer of string";
void * temp; //temp可以存放其他任何类型的指针(地址)
temp=array; // temp 的指针类型
cout<<array<<endl;
cout<<temp<<endl;
运行结果:
I am the pointer of string
0x0042510C (这个值就是array指针变量所存储的值)
2.但是不能将void *类型的值赋给其他既定的类型 ,除非 经过显示转换:
例如:
int a=20;
int * pr=&a;
void *p;
pr=p //error,不能将空的类型赋给int *
而在c里,由于编译时刻类型检查是非强制性的,所以有些不同.例如:
// this is a c++program
...
void *pv;
int ival = 1024;
int *pi = &ival;
pv = pi; //ok: pv指向ival
pc = pv; //wrong: 缺少标准类型转换
...
那么,假如以上是个c程序呢?调试一下就知道了.