|
void *從本質(zhì)上講是一種指針的類(lèi)型,就像 (char *)、(int *)類(lèi)型一樣.但是其又具有
特殊性,它可以存放其他任何類(lèi)型的指針類(lèi)型:例如:
char *array="I am the pointer of string";
void *temp; //temp可以存放其他任何類(lèi)型的指針(地址)
temp=array; // temp 的指針類(lèi)型
cout<<array<<endl;
cout<<temp<<endl;
cout<<(char*)temp<<endl;
運(yùn)行結(jié)果:
I am the pointer of string
0x0042510C (這個(gè)值就是array指針變量所存儲(chǔ)的值)
I am the pointer of string
2.但是不能將void *類(lèi)型的值賦給其他既定的類(lèi)型 ,除非 經(jīng)過(guò)顯示轉(zhuǎn)換:
例如:
int a=20;
int * pr=&a;
void *p;
pr=p //error,不能將空的類(lèi)型賦給int *
pr=(int *)p; //ok,經(jīng)過(guò)轉(zhuǎn)換
|
|