对于void 真正发挥的作用在于:
1、对函数返回的限定;
2、对函数参数的限定
但是,在编写的代码中有的人也会想到用void,因为有了void 我们就不会受到类型的多方面的限制了。
但是使用void申请变量的时候,在C/C++中编译器是会报错。
void 的事例代码
- #include<stdio.h>
- int main()
- {
- void ok;
- return 0;
- }
void 指针
1、与另外一个指针进行比较;
2、想函数传递void *指针和返回void *指针;
3、给另一个void *指针赋值;
4、不允许使用void*指针操纵它指向的对象;
void * 代码
- #include<iostream>
- using namespace std;
- struct ok
- {
- int a;
- char c;
- };
- typedef struct ok ok_struct;
- int main()
- {
- ok_struct data;
- void * ptr =(void*)&data;
- data.a=1;
- data.c = 'r';
- cout<<"data = "<<data.a<<" "<<data.c;
- cout<<"Dptr = "<<((ok_struct*)ptr)->a<<" "<<((ok_struct*)ptr)-> ;
- return 1;
- }