当前位置:网站首页>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 .
边栏推荐
- How do I write Flask's excellent debug log message to a file in production?
- signed、unsigned关键字
- volatile关键字
- 【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
- NetXpert XG2帮您解决“布线安装与维护”难题
- The nearest common ancestor of binary (search) tree ●●
- ThreadLocal详解
- rust知识思维导图xmind
- 图像的spatial domain 和 frequency domain 图像压缩
- Web APIs DOM 时间对象
猜你喜欢
signed、unsigned关键字
Mysql database basic operations DML
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
PVL EDI project case
Aardio - 利用customPlus库+plus构造一个多按钮组件
二叉(搜索)树的最近公共祖先 ●●
[leetcode] 19. Delete the penultimate node of the linked list
在IPv6中 链路本地地址的优势
The SQL response is slow. What are your troubleshooting ideas?
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
随机推荐
Puppeteer连接已有Chrome浏览器
Chapter 4: talk about class loader again
MySQL ---- first acquaintance with MySQL
中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
变量与“零值”的比较
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
手写ABA遇到的坑
Aardio - 利用customPlus库+plus构造一个多按钮组件
Should novice programmers memorize code?
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
Management background --4, delete classification
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
2022-07-05 使用tpcc对stonedb进行子查询测试
i. Mx6ull build boa server details and some of the problems encountered
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
Assembly and interface technology experiment 5-8259 interrupt experiment
【雅思口语】安娜口语学习记录part1
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
Inno Setup 打包及签名指南