标题: void *用法 [打印本页]

作者: 51hei社区    时间: 2016-1-16 07:00
标题: void *用法
                        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 *
                                 pr=(int *)p;  //ok,经过转换







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1