当前位置:网站首页>C語言0長度數組的妙用
C語言0長度數組的妙用
2022-06-27 11:06: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來支持才能編譯通過。

边栏推荐
- NVME2.0协议——新特性
- 【TcaplusDB知识库】TcaplusDB系统管理介绍
- Design and Simulation of direct torque control system for induction motor (motion control matlab/simulink)
- [tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)
- 【TcaplusDB知识库】TcaplusDB OMS业务人员权限介绍
- Concepts of concurrency, parallelism, asynchronism, synchronization, multithreading and mutual exclusion
- JS client storage
- [cloud enjoys freshness] community weekly · vol.68- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + business realization
- Openpyxl table reading instance
- 杰理之睡眠以后定时唤醒系统继续跑不复位【篇】
猜你喜欢

KDD 2022 | 基于分层图扩散学习的癫痫波预测

Native JS implements page scroll bar loading data and page drop-down loading content

iMeta:高颜值绘图网站imageGP+视频教程合集,已被引360次(220625更新)

How to deploy jupyterlab in methodot?

Oracle-分组统计查询
![[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)](/img/74/a645742a8e135b32154859be956760.png)
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)

【TcaplusDB知识库】TcaplusDB Tmonitor模块架构介绍

Interview shock 60: what will cause MySQL index invalidation?

居家办公竟比去公司上班还累? | 社区征文

杰理之串口通信 串口接收IO需要设置数字功能【篇】
随机推荐
信息学奥赛一本通 1332:【例2-1】周末舞会
数据库之元数据
Deep understanding of happens before principle
go-zero微服务实战系列(七、请求量这么高该如何优化)
If you find any loopholes later, don't tell China!
Concepts of concurrency, parallelism, asynchronism, synchronization, multithreading and mutual exclusion
【TcaplusDB知识库】TcaplusDB OMS业务人员权限介绍
[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch / 3.4.1 spanning tree protocol (STP)
[tcapulusdb knowledge base] tcapulusdb cluster management introduction
直播电子商务应用程序开发需要什么基本功能?未来发展前景如何?
[tcapulusdb knowledge base] Introduction to new models of tcapulusdb
堆-堆排序-TopK
Go zero micro Service Practice Series (VII. How to optimize such a high demand)
VPT Model Video Explanation
【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍
go-zero微服务实战系列(七、请求量这么高该如何优化)
iMeta:高颜值绘图网站imageGP+视频教程合集,已被引360次(220625更新)
Leetcode 729. 我的日程安排表 I(牛逼,已解决)
Ubuntu手動安裝MySQL