当前位置:网站首页>C语言0长度数组的妙用
C语言0长度数组的妙用
2022-06-27 10:57:00 【fhqlongteng】
1、0长度数组
0长度数组的意思是数组的长度为0,比如int a[0],它编译后不占用内存空间,即sizeof(a) = 0。
C99规定可以为变长数组 int len ; int array[len];也就是说数组是程序运行时候才指定数组的大小。常见的是进行输入数据进行初始化。
GCC直接支持零长度数组,int a[0];零长度数组有一个特点就是不占用内存空间。0长度数组很少单独使用,一般是用在结构体中,不占用结构体长度,用于分配连续内存时使用。
2、0长度数组在结构体中的使用
0长度数组常用在结构体中,如下代码就是一个结构体中带有0长度数组。此结构体sizeof(struct rndis_query_msg) = 7个字=28个字节长度,OIDInputBuffer[0]在结构体中未占用空间。
/* Remote NDIS Query Message */
struct rndis_query_msg
{
rt_uint32_t MessageType;
rt_uint32_t MessageLength;
rt_uint32_t RequestId;
rt_uint32_t Oid;
rt_uint32_t InformationBufferLength;
rt_uint32_t InformationBufferOffset;
rt_uint32_t Reserved;
rt_uint8_t OIDInputBuffer[0];
};
typedef struct rndis_query_msg* rndis_query_msg_t;OIDInputBuffer主要用在分配内存时访问结构体成员变量Reserved后面连续内存使用。比如要申请一个struct rndis_query_msg结构占用的内存空间,并且OIDInputBuffer指向的内存具有10个字节的空间可以使用。代码可像如下编写。
rndis_query_msg_t prndis = NULL;
prndis = malloc(sizeof(struct rndis_query_msg) + 10);
if(prndis)
{
for(i = 0; i < 10; i++)
{
prndis->OIDInputBufferp[i] = i;
}
}
/*如果此文对你有帮助,请我喝茶--加我vx:longtengweixin*/3、支持的编译器
0长度的数组的写法在IAR编译器,GCC编译器中支持。但是在keil编译器默认是不支持的,即使打开C99的设置也不好使。keil软件中需要在编译选项中增加--gcc来支持才能编译通过。

边栏推荐
- 隐私计算FATE-离线预测
- Istio related information
- Concepts of concurrency, parallelism, asynchronism, synchronization, multithreading and mutual exclusion
- [tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)
- 学习笔记之——数据集的生成
- 【TcaplusDB知识库】TcaplusDB Tmonitor模块架构介绍
- 基于swiftadmin极速后台开发框架,我制作了菜鸟教程[专业版]
- deep learning statistical arbitrage
- Go zero micro Service Practice Series (VII. How to optimize such a high demand)
- “全班29人24人成功读研”冲上热搜!剩下的5个人去哪了?
猜你喜欢

Oracle-分组统计查询

杰理之串口通信 串口接收IO需要设置数字功能【篇】

Future & CompletionService

【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(一)

Design and Simulation of direct torque control system for induction motor (motion control matlab/simulink)

ECMAScript 6(es6)
Eureka核心源码解析

Codeforces Round #786 (Div. 3) ABCDE
![File name setting causes an error to be written to writelines: oserror: [errno 22] invalid argument](/img/08/2d4f425e6941af35616911672b6fed.png)
File name setting causes an error to be written to writelines: oserror: [errno 22] invalid argument

【Methodot 专题】什么样的低代码平台更适合开发者?
随机推荐
21: Chapter 3: develop pass service: 4: further improve [send SMS, interface]; (in [send SMS, interface], call Alibaba cloud SMS service and redis service; a design idea: basecontroller;)
15+城市道路要素分割应用,用这一个分割模型就够了!
记一次 .NET 某物管后台服务 卡死分析
NVME2.0协议——新特性
Learning notes - data set generation
杰理之一直喂狗会频繁开关中断导致定时器【篇】
[tcapulusdb knowledge base] tcapulusdb Model Management Introduction
Redis 分布式锁15问,看看你都掌握了哪些?
杰理之增加一个输入捕捉通道【篇】
中科院微生物所招聘青年PI 20位,2百万安家费,千万启动经费(长期有效)
Deep learning in finance in cross sectional sectional predictions for random forests
Imeta: a collection of imagegp+ video tutorials of high-value drawing websites, which has been cited 360 times (220625 updates)
【Methodot 专题】什么样的低代码平台更适合开发者?
Uniform Asymptotics by Alexei
【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
【TcaplusDB知识库】TcaplusDB Tmonitor模块架构介绍
直播電子商務應用程序開發需要什麼基本功能?未來發展前景如何?
嵌入式软件架构设计-模块化
KDD 2022 | 基于分层图扩散学习的癫痫波预测
[tcapulusdb knowledge base] Introduction to tcapulusdb table data caching