当前位置:网站首页>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 .
边栏推荐
- Senior soft test (Information System Project Manager) high frequency test site: project quality management
- 2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
- pytorch_ Yolox pruning [with code]
- 2022-07-05 use TPCC to conduct sub query test on stonedb
- 小程序系统更新提示,并强制小程序重启并使用新版本
- NPDP认证|产品经理如何跨职能/跨团队沟通?
- 3DMAX assign face map
- 【无标题】
- ThreadLocal详解
- HDR image reconstruction from a single exposure using deep CNN reading notes
猜你喜欢
Build op-tee development environment based on qemuv8
NPDP认证|产品经理如何跨职能/跨团队沟通?
PVL EDI project case
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
CCNA Cisco network EIGRP protocol
labelimg的安装与使用
[leetcode daily clock in] 1020 Number of enclaves
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
Senior soft test (Information System Project Manager) high frequency test site: project quality management
【编译原理】做了一半的LR(0)分析器
随机推荐
SQL server generates auto increment sequence number
Attack and defense world miscall
sizeof关键字
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
That's why you can't understand recursion
【无标题】
MySQL约束的分类、作用及用法
Daily question 1: force deduction: 225: realize stack with queue
Clip +json parsing converts the sound in the video into text
第4章:再谈类的加载器
2021 geometry deep learning master Michael Bronstein long article analysis
云原生技术--- 容器知识点
C# 三种方式实现Socket数据接收
机试刷题1
Self made j-flash burning tool -- QT calls jlinkarm DLL mode
[leetcode daily clock in] 1020 Number of enclaves
Aardio - 利用customPlus库+plus构造一个多按钮组件
Comparison between variable and "zero value"
枚举与#define 宏的区别
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices