当前位置:网站首页>【C语法】void*浅说
【C语法】void*浅说
2022-07-25 22:10:00 【来碗豆腐脑】
void,很常见,理解为空我自己认为没什么不妥(一般函数定义时只使用()其实并不能代表(void),前者是可以空,后者是必须空)void*,库函数也算常见,我自己没用过,之间一直理解为空指针,这两天才知颇有偏差,其实它叫通用指针,只是在未初始化时候是空的,一旦初始化后再叫空指针肯定就别扭了,毕竟内容已经不空了,空的是指针类型,而不是指针值。
- 什么是通用指针?
顾名思义,就是通用的指针,本身没有类型,类型为空,内容可以不空,任何其他指针都能赋值给它,而不会有警告,所以叫通用指针(在gcc11.2中,它赋值给其他指针也不会警告,但不要这样使用)。
因为类型特殊,所以引用的时候要强转为对应的类型后再引用,赋值的时候虽然也可以直接赋值给实际的同类型指针(gcc11.2这样做默认没有任何警告,clangd LSP服务器也检查不到错误),但不建议这么做。强制转换可以触发编译器的指针类型赋值时的类型检查,所以最好都在使用前强转,如下是一个示例程序和运行结果:
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
char a = 2;
char *p = &a;
void* vp = NULL;
char *pc = NULL;
vp = p; //char*指针直接赋值给通用指针
*(char*)vp = 1; //通用指针强转后也可以引用
p = (char*)vp; //其实直接赋值也可以,但强转后更安全,否则没有指针赋值的类型检查
printf("%d, %d",*(char*)vp, *p); //打印时可以看到通用指针引用正常
pc = (char*)malloc(100*sizeof(char)); //返回的通用指针通常都做了强制转换,已经是默认的写法
free(pc);
getchar();
return 0;
}

标准库函数中大量使用了void*,比如C语言提供的内存管理,再看就更理解为什么用void*了:
//申请固定大小内存
void *malloc(int num);
//内存释放
void free(void *address);
边栏推荐
- How many bytes does Boolean occupy?
- Basic knowledge in the project
- Redis基础2(笔记)
- 2day
- JSP novice
- 启牛商学院和微淼商学院哪个靠谱?老师推荐的开户安全吗?
- SQL基本语句 DQL select与提取 DML插入删除
- Ts:typera code fragment indentation display exception (resolved) -2022.7.24
- Redis内存淘汰机制?
- 『Skywalking』. Net core fast access distributed link tracking platform
猜你喜欢

Randomly generate 10 (range 1~100) integers, save them to the array, and print the array in reverse order. And find the average value, the maximum value and the subscript of the maximum value, and fin

Wechat applet application development competition works comprehensive development record - Jinlu cultural tourism (cloud development - Overview)

Having met a tester with three years' experience in Tencent, I saw the real test ceiling

Flex layout

Virtual memory and disk

Basic knowledge in the project

『Skywalking』. Net core fast access distributed link tracking platform

ansible+Crontab批部署巡检

Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (judgment question) June 2022

动画曲线天天用,你能自己整一个吗?看完这篇你就会了!
随机推荐
Detailed summary of C language game dual cache to solve the flash screen problem [easy to understand]
Internship: writing common tool classes
What should I do if I encounter the problem of verification code during automatic testing?
Redis memory elimination mechanism?
JSP novice
JSP nine built-in objects
Nuclear power plants strive to maintain safety in the heat wave sweeping Europe
开户就可以购买收益在百分之六以上的理财产品了吗
All you want to know about interface testing is here
2day
Jenkins+svn configuration
数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir
ansible+Crontab批部署巡检
[fan Tan] after the arrival of Web3.0, where should testers go? (ten predictions and suggestions)
Open source RSS subscriber freshrss
The testing work is not valued. Have you changed your position?
Dovecot set mailbox quota
五种分配方式是否会产生内部碎片、外部碎片
[hand tear STL] BitSet (bitmap), bloom filter
What is redis? Briefly describe its advantages and disadvantages