当前位置:网站首页>Void keyword
Void keyword
2022-07-06 22:31:00 【It's Beichen not too PI acridine】
void keyword
void Literally, it means “ Empty type ”,void * Then for “ Null type pointer ”,void * Can point to any type of data .
void Almost only “ notes ” And limiting the role of procedures , Because no one ever defines a void Variable .
void The real role is :
(1) A restriction on the return of a function ;
(2) Restrictions on function parameters .
as everyone knows , If the pointer p1 and p2 Same type of , Then we can directly p1 and p2 They assign values to each other ; If p1 and p2 Point to different data types , You must use the cast operator to convert the type of the pointer on the right side of the assignment operator to the type of the pointer on the left .
float *p1;
int *p2;
p1 = (float *)p2;
and void * Is different , Any type of pointer can be assigned directly to it , There is no need to cast :
void *p1;
int *p2;
p1 = p2;
But that doesn't mean ,void * You can also assign pointers to other types without having to cast them . because “ Empty type ” can
With inclusiveness “ There are types ”, and “ There are types ” We can't tolerate “ Empty type ”.
The following statement compilation error :
void *p1;
int *p2;
p2 = p1;
void Modify function return values and parameters
1. If the function does not return a value , Then it should be declared as void type .
stay C In language , Any function without return value type restriction , Will be handled by the compiler as a return integer value . But many programmers mistake it for void type .
add ( int a, int b )
{
return a + b;
}
int main(int argc, char* argv[])
{
printf ( "2 + 3 = %d", add ( 2, 3) );
}
The result of program running is output : 2 + 3 = 5
This shows that the function without return value description is indeed int function .
therefore , To avoid chaos , We're writing C The program , For any function, its type must be specified one by one . If the function does not return a value , Be sure to state that void type . This is the need for good readability of the program , It is also the requirement of programming standardization . in addition , add void After type declaration , Can also play the role of code “ Self annotation ” effect . The so-called code “ Self annotation ” That is, the code can annotate itself .
2. If the function has no arguments , Then it should be declared that the parameter is void
int function(void)
{
return 1;
}
It is illegal to make the following call :function(2);
The parameter of the function is void This function takes no arguments .
#include "stdio.h"
fun()
{
return 1;
}
int main()
{
printf("%d",fun(2));
getchar();
}
Compile correctly and output 1, This explanation , stay C In language , You can pass any type of parameter to a function without parameters .
void The pointer
1. Be careful and use carefully void Pointer types .
according to ANSI(American National Standards Institute) standard , Not right void Pointer for algorithm operation .
void * pvoid;
pvoid++; //ANSI: error
pvoid += 1; //ANSI: error
ANSI The reason why the standard is so recognized , It's because it insists on : The pointer for algorithm operation must be determined to know the size of the data type it points to . In other words, you must know the exact value of the memory destination address .
int *pint;
pint++; //ANSI: correct
But famous GNU(GNU’s Not Unix The recursive abbreviation of ) I don't think so , It specifies void * The algorithm operation and char * Agreement . So the following statement is in GNU It's all right in the compiler :
pvoid++; //GNU: correct
pvoid += 1; //GNU: correct
In actual programming , To conform to ANSI standard , And improve the portability of the program , We can write
Code that performs the same function :
void * pvoid;
(char *)pvoid++; //ANSI: correct ;GNU: correct
(char *)pvoid += 1; //ANSI: error ;GNU: correct
GNU and ANSI There are also some differences , Overall speaking ,GNU a ANSI more “ to open up ”, Provides support for more Syntax . But when we're actually designing , We should try our best to conform to ANSI standard .
If the argument to a function can be a pointer of any type , Then it should be declared that the parameter is void *.
Typical examples are memory operation functions memcpy and memset The function prototypes of are :
void * memcpy(void *dest, const void *src, size_t len);
void * memset ( void *buffer, int c, size_t num );
such , Any type of pointer can be passed in memcpy and memset in , This also truly reflects the meaning of memory operation function , Because the object it operates on is just a piece of memory , No matter what type of memory it is .
memset Accept any type of pointer
int IntArray_a[100];
memset (IntArray_a, 0, 100*sizeof(int) ); // take IntArray_a clear 0
memcpy Accept any type of pointer
int destIntArray_a[100], srcintarray_a[100];
// take srcintarray_a Copy to destIntArray_a
memcpy (destIntArray_a, srcintarray_a, 100*sizeof(int) );
memcpy and memset Function also returns void * type
void Can't represent a real variable
Because memory space must be allocated when defining variables , Definition void Type variable , How much memory does the compiler allocate .
The following code attempts to make void Represents a real variable , So it's all wrong code :
void a; // error
function(void a); // error
void It's just an abstract need , If you understand object-oriented correctly “ Abstract base class ” The concept of , And it's easy to understand void data type . Just as you can't define an instance of an abstract base class , We can't define a void( Let's call it by analogy void by “ Abstract data types ”) Variable .
边栏推荐
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- C# 三种方式实现Socket数据接收
- Data storage (1)
- [IELTS speaking] Anna's oral learning record part1
- 3DMAX assign face map
- Management background --3, modify classification
- i. Mx6ull build boa server details and some of the problems encountered
- 在IPv6中 链路本地地址的优势
- GD32F4XX串口接收中断和闲时中断配置
- ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
猜你喜欢
【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
NPDP认证|产品经理如何跨职能/跨团队沟通?
pytorch_YOLOX剪枝【附代码】
Chapter 3: detailed explanation of class loading process (class life cycle)
NPDP certification | how do product managers communicate across functions / teams?
0 basic learning C language - digital tube
C# 三种方式实现Socket数据接收
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
网络基础入门理解
Export MySQL table data in pure mode
随机推荐
剑指offer刷题记录1
每日一题:力扣:225:用队列实现栈
剪映+json解析将视频中的声音转换成文本
sizeof关键字
NetXpert XG2帮您解决“布线安装与维护”难题
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
Data storage (1)
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
Windows Auzre 微软的云计算产品的后台操作界面
[linear algebra] determinant of order 1.3 n
2022-07-05 stonedb sub query processing parsing time analysis
Adavit -- dynamic network with adaptive selection of computing structure
Gd32f4xx serial port receive interrupt and idle interrupt configuration
Anaconda installs third-party packages
PVL EDI project case
Comparison between variable and "zero value"
MySQL----初识MySQL
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
Improving Multimodal Accuracy Through Modality Pre-training and Attention