標(biāo)題: void *用法 [打印本頁]

作者: 51hei社區(qū)    時(shí)間: 2016-1-16 07:00
標(biāo)題: void *用法
                        void *從本質(zhì)上講是一種指針的類型,就像 (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;
運(yùn)行結(jié)果:
                 I am the pointer of string
                 0x0042510C (這個(gè)值就是array指針變量所存儲(chǔ)的值)
                 I am the pointer of string

2.但是不能將void *類型的值賦給其他既定的類型 ,除非 經(jīng)過顯示轉(zhuǎn)換:
                       例如:
                                int a=20;
                                int * pr=&a;
                                 void *p;
                                 pr=p      //error,不能將空的類型賦給int *
                                 pr=(int *)p;  //ok,經(jīng)過轉(zhuǎn)換







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1