当前位置:网站首页>[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);
边栏推荐
- Selenium basic use and use selenium to capture the recruitment information of a website (continuously updating)
- 2022 the latest software tests eight part essay. Whether you can offer depends on how you recite it
- 『SignalR』. Net using signalr for real-time communication
- Why does redisv6.0 introduce multithreading?
- [test development methodology] experience of test development platform PK - choice
- Solutions to the failure of win key in ikbc keyboard
- C language: random generated number + selective sorting
- TFrecord写入与读取
- SQL基本语句 DQL select与提取 DML插入删除
- 五种分配方式是否会产生内部碎片、外部碎片
猜你喜欢

TFrecord写入与读取

On the difference between break and continue statements

H5幸运刮刮乐抽奖 免公众号+直运营

Open source RSS subscriber freshrss

Selenium basic use and use selenium to capture the recruitment information of a website (continuously updating)

Advanced database · how to add random data for data that are not in all user data - Dragonfly Q system users without avatars how to add avatar data - elegant grass technology KIR

After 2 years of functional testing, I feel like I can't do anything. Where should I go in 2022?

手机端微信发朋友圈功能测试点总结

如何实现一个App应用程序,限制用户时间使用?

如何将一个域名解析到多个IP地址?
随机推荐
Redis基础2(笔记)
synchronized与volatile
C language: random generated number + selective sorting
The testing work is not valued. Have you changed your position?
测试工作不受重视,你换位思考了吗?
MySQL --- 子查询 - 列子查询(多行子查询)
信息安全建设原则指导
H5 lucky scratch lottery free official account + direct operation
『Skywalking』.NET Core快速接入分布式链路追踪平台
Advanced database · how to add random data for data that are not in all user data - Dragonfly Q system users without avatars how to add avatar data - elegant grass technology KIR
jenkins+SVN配置
Redis memory elimination mechanism?
Why does redis choose single thread?
Ts:typera code fragment indentation display exception (resolved) -2022.7.24
虚拟内存与磁盘
JSP初识
C language: random generated number + bubble sort
How to implement an app application to limit users' time use?
Preliminary study on Tesseract OCR
Which is reliable between qiniu business school and WeiMiao business school? Is it safe to open an account recommended by the teacher?