void数据类型相关概念以及使用方式
void数据类型
- void 字面意思是“无类型”,void* 无类型指针,无类型指针可以指向任何类型的数据
- void定义变量是没有任何意义的,当你定义void a,编译器 会报错的。
void真正用在以下两个方面:
- 对函数返回的限定;
- 对函数参数的限定;
void的使用:
- 无类型,不可以创建变量,无法分配内存
- 限定函数返回值
- 限定函数中的参数列表
- void* 万能指针,不需要强制类型转换 给其他指针赋值
代码示例:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//1.无类型是不可以创建变量的
void test01()
{
//void a=10;//编译器直接报错,因为不知道给a分配多少内存
}
void func()
{
//return 10;
}
void test02()
{
//printf("%d\n",func());
}
//3.限定函数参数列表
int func2(void)
{
return 10;
}
void test03()
{
//printf("%d\n",func2(10));
}
//4.void* 万能指针
void test04()
{
void* p = NULL;
int* pInt = NULL;
char*pChar = NULL;
//pChar = pInt;
pChar = p;// 万能指针 可以不需要强制类型转换就可以给等号左边赋值
printf("size of void*=%d\n",sizeof(p));
}
int main()
{
//test02();
//test03();
test04();
return EXIT_SUCCESS;
}
更多文章,敬请关注微信公众号:YQ编程