当前位置:网站首页>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 .
边栏推荐
- Build op-tee development environment based on qemuv8
- 基于 QEMUv8 搭建 OP-TEE 开发环境
- Assembly and interface technology experiment 5-8259 interrupt experiment
- leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
- poj 1094 Sorting It All Out (拓扑排序)
- pytorch_ Yolox pruning [with code]
- LeetCode 练习——剑指 Offer 26. 树的子结构
- Plafond du tutoriel MySQL, bien collecté, regardez lentement
- Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
- UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
猜你喜欢
Export MySQL table data in pure mode
Aardio - 利用customPlus库+plus构造一个多按钮组件
CocosCreator+TypeScripts自己写一个对象池
Self made j-flash burning tool -- QT calls jlinkarm DLL mode
Mysql database basic operations DML
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
(18) LCD1602 experiment
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
[linear algebra] determinant of order 1.3 n
网络基础入门理解
随机推荐
剪映+json解析将视频中的声音转换成文本
二分图判定
volatile关键字
变量与“零值”的比较
新手程序员该不该背代码?
signed、unsigned关键字
Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
Leetcode exercise - Sword finger offer 26 Substructure of tree
在IPv6中 链路本地地址的优势
[leetcode daily clock in] 1020 Number of enclaves
sizeof关键字
基於 QEMUv8 搭建 OP-TEE 開發環境
SQL Server生成自增序号
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
Assembly and interface technology experiment 5-8259 interrupt experiment
做国外LEAD2022年下半年几点建议
Chapter 4: talk about class loader again
第3章:类的加载过程(类的生命周期)详解
Windows Auzre 微软的云计算产品的后台操作界面
OpenCV VideoCapture. Get() parameter details