当前位置:网站首页>[C syntax] void*
[C syntax] void*
2022-07-25 22:12:00 【A bowl of bean curd】
void, Very common , I don't think it's wrong to understand it as empty ( Generally, only () In fact, it doesn't represent (void), The former can be empty , The latter must be empty )void*, Library functions are also common , I didn't use it myself , It has always been understood as a null pointer , These two days I know quite a deviation , In fact, it is called universal pointer , It is only empty when uninitialized , It must be awkward to call null pointer after initialization , After all, the content is not empty , Null is the pointer type , Instead of pointer value .
- What is a general pointer ?
seeing the name of a thing one thinks of its function , Is a general pointer , There is no type in itself , The type is empty , The content can not be empty , Any other pointer can be assigned to it , Without warning , So it is called universal pointer ( stay gcc11.2 in , It is assigned to other pointers without warning , But don't use it like this ).
Because of the special type , Therefore, when quoting, it is necessary to forcibly convert to the corresponding type before quoting , Although it can also be directly assigned to the actual pointer of the same type when assigning values (gcc11.2 By default, there is no warning ,clangd LSP The server cannot detect the error ), But it's not recommended . Cast can trigger type checking when the compiler assigns pointer types , So it's best to turn strongly before using , Here is a sample program and running results :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
char a = 2;
char *p = &a;
void* vp = NULL;
char *pc = NULL;
vp = p; //char* The pointer is directly assigned to the general pointer
*(char*)vp = 1; // The general pointer can also be referenced after strong rotation
p = (char*)vp; // In fact, direct assignment can also , But it's safer after strong rotation , Otherwise, there is no type check for pointer assignment
printf("%d, %d",*(char*)vp, *p); // When printing, you can see that the general pointer reference is normal
pc = (char*)malloc(100*sizeof(char)); // The returned generic pointer is usually cast , It is already the default writing
free(pc);
getchar();
return 0;
}

Standard library functions are heavily used void*, such as C Memory management provided by language , Look again and you will understand why you use void* 了 :
// Apply for fixed size memory
void *malloc(int num);
// Memory free
void free(void *address);
边栏推荐
- On the difference between break and continue statements
- 成为比开发硬气的测试人,我都经历了什么?
- Nuclear power plants strive to maintain safety in the heat wave sweeping Europe
- Get together for ten years, tell your story, millions of gifts are waiting for you
- 字节跳动技术面都过了,结果还是被刷了,问HR原因竟是。。。
- Wechat card issuing applet source code - automatic card issuing applet source code - with flow main function
- SQL中in的用法 DQL 查询
- 虚拟内存与磁盘
- 【GO基础02】第一个程序
- 在进行自动化测试,遇到验证码的问题,怎么办?
猜你喜欢

Solutions to the failure of win key in ikbc keyboard

What have I experienced to become a harder tester than development?

3day

如何将一个域名解析到多个IP地址?

Don't know mock test yet? An article to familiarize you with mock

微信发卡小程序源码-自动发卡小程序源码-带流量主功能

2年功能测试,却感觉自己什么都不会,2022我该何去何从?

QML module not found

2 lines of code to generate a solid desktop background

JMeter websocket interface test
随机推荐
JMeter websocket接口测试
The second short contact of gamecloud 1608
C language left value and right value description [easy to understand]
JSP nine built-in objects
Tfrecord write and read
Imitation Tiktok homepage interface
启牛商学院和微淼商学院哪个靠谱?老师推荐的开户安全吗?
Application of breakthrough thinking in testing work
Wechat applet application development competition works comprehensive development record - Jinlu cultural tourism (cloud development - Overview)
What is partition and barrel division?
Usage of in in SQL DQL query
Why does redisv6.0 introduce multithreading?
TS:typora代码片段缩进显示异常(已解决)-2022.7.24
Internship: writing common tool classes
6-18 vulnerability exploitation - backdoor connection
在进行自动化测试,遇到验证码的问题,怎么办?
Nuclear power plants strive to maintain safety in the heat wave sweeping Europe
Redis内存淘汰机制?
Ts:typera code fragment indentation display exception (resolved) -2022.7.24
【汇编语言01】基础知识