void
来源:互联网 发布:将java文件打包成jar 编辑:程序博客网 时间:2024/05/21 07:52
1 void修饰函数返回值和参数
- 如果函数没有返回值,那么应该将其声明为void;
- 如果函数没有参数,应该声明其参数为void;
- void修饰函数返回值和参数是为了表示无。
注意:形参为void时代表不接受任何参数,没有写参数意味着可以接受任意多的参数 ,没有写返回值默认返回值为int类型并且值为1。
2 不存在void变量
C语言没有定义void究竟是多大内存的别名,没有void的标尺,无法在内存中裁剪出void对应的变量。
小贴士:
- ANSI C:标准C语言的规范
- 扩展C:在ANSI C 的基础上进行了扩充
在gcc中输出结果为1。
3 void指针的意义
- C语言规定只有相同类型的指针才可以相互赋值。
- void *指针作为左值用于“接收”任意类型的指针。
- void *指针作为右值使用时需要进行强制类型转换(不进行强转也不会给出任何警告信息)。
实例分析:通过void* 实现MemSet函数
#include <stdio.h>void MemSet(void* src, int length, unsigned char n){ unsigned char* p = (unsigned char*)src; int i = 0; for(i=0; i<length; i++) { p[i] = n; }}int main(){ int a[5]; int i = 0; MemSet(a, sizeof(a), 0); for(i=0; i<5; i++) { printf("%d\n", a[i]); } return 0;}
4 小结
- void是一种抽象的数据类型。
- void类型不能用于定义变量。
- void类型用于声明函数无参数。
- void类型用于声明函数无返回值。
- 可以定义void*类型的指针。
- void*类型的指针可以接受任意类型的指针值。
阅读全文
0 0
- void *
- void
- void
- void
- void
- void
- void*
- void
- void
- void
- void
- void
- void
- void
- (void**)&
- void *
- void
- void*
- zoj3987—Numbers(贪心)
- 前端框架Vue(16)——vue-i18n ,vue项目中如何实现国际化
- jsp中日期格式化及小数点格式化
- gradle配置没错,但是就是启动有错误的解决办法
- map和reduce任务各参数参考
- void
- RocketMQ简介
- python基本操作总结(一)
- Gym小记(四)
- APP界面设计建议
- 复杂性思维第二版 二、图
- git使用小结
- 数据结构(python)
- Hive基本操作语句