目录
[void * 万能指针](#void * 万能指针)
描述作用
1.void 是无类型,void 是不允许定义变量的,可以查看 "void定义变量案例"
2.void限定函数返回值
3.void限定函数参数列表
4.void * 万能指针

vold定义变量案例

//void是不允许定义变量的
int test1()
{
void i = 10; //编译器不知道给多少内存,编译器不会让代码通过编译
printf("test1\n");
}
void 是不允许定义变量的: 比如 void i = 10; 这样是直接编译报错。因为编译器不知道给多少内存
void限定函数返回值
案例1
func()
{
return 10;
}
int test2() {
printf("test2 func : %d\n\n",func());
}
运行结果

可以看上面案例,我们在返回的位置什么也没加,照样可以运行,而且运行结果也正常返回了10。对于C语言来说,不写返回值也可以调出来。
案例2

从案例1和案例2 可以看出,如果写void那么就会报错,可以限制返回值。这里案例二报错原因是有为 func函数是vold 用%d来解析,解析不出来,编译器抛出错误提示
void限定函数参数列表
案例1
int func1()
{
return 10;
}
int test3() {
func1(10);
printf("test2 \n\n");
}

运行结果

从什么代码来看我们如果在函数参数列表中不做任何限制,调用函数也是可以正常运行的。C语言不会取检测这种语病。
案例2
int func1(void)
{
return 10;
}
int test3() {
func1(10);
printf("test3 \n\n");
}
案例1 与 案例2的区别是在函数传参数的过程中加入了一个void。那么可以来看看运行结果

这样写运行结果也没有问题。但是在编译过程中会提示程序员 error C2197的异常提示。有总比没有好。如果连提示都没有,我们根本无法找到代码异常

void * 万能指针
直接看案例,对应案例来说
案例1
void test4() {
void* p = NULL;
int* pInt = NULL;
char* pChar = NULL;
pInt = pChar;
printf("test4 \n\n");
}
运行结果:

编译提示

从上面代码来看不同类型的指针直接互相传递编译器是允许的,只会做出提示,并不会报错。提示类型不兼容。
如果想让编译器不提示,那么就需要强制的进行比如:

案例2
void test4() {
void* p = NULL;
int* pInt = NULL;
char* pChar = NULL;
pInt = p;//如果直接使用p那么就不需要强转换,直接使用p 万能指针即可,编译器也不会进行提示
printf("test4 * p size of %d \n\n",sizeof(p));
}
万能指针不需要强制转换,编译器也不会报错。那么我们可以看一下指针的大小,指针大小一般是8或者4,要看平台(操作系统)分配。

总结
void,不可以创建变量,无法分配内存。
void,可以限制函数返回值。
void,可以限制函数中的参数列表。
void,void*万能指针,可以不需要强制类型转换。