当前位置:网站首页>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來支持才能編譯通過。

边栏推荐
- JS client storage
- Institute of Microbiology, Chinese Academy of Sciences recruited 20 young PI, with a resettlement fee of 2million yuan and a start-up fund of 10million yuan (long-term effective)
- Uniform Asymptotics by Alexei
- Review of last week's hot spots (6.20-6.26)
- 【TcaplusDB知识库】TcaplusDB常规单据介绍
- 【TcaplusDB知识库】TcaplusDB机型管理介绍
- 数据库之元数据
- 【TcaplusDB知识库】TcaplusDB系统管理介绍
- File name setting causes an error to be written to writelines: oserror: [errno 22] invalid argument
- Leetcode 729. 我的日程安排表 I(提供一种思路)
猜你喜欢

【Methodot 专题】什么样的低代码平台更适合开发者?
![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

Oracle group statistics query

【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍

How to deploy jupyterlab in methodot?

Mail system (based on SMTP protocol and POP3 protocol -c language implementation)

Leetcode 729. My schedule I (awesome, solved)

Future & CompletionService

杰理之DAC输出方式设置【篇】

红包雨: Redis 和 Lua 的奇妙邂逅
随机推荐
Review of last week's hot spots (6.20-6.26)
Queue, two-way queue, and its application
0基础了解电商系统如何对接支付渠道
Design and Simulation of direct torque control system for induction motor (motion control matlab/simulink)
“互联网+”大赛命题火热对接中 | 一图读懂百度38道命题
Codeforces Round #786 (Div. 3) ABCDE
One copy ten, CVPR oral is accused of plagiarizing a lot
实验笔记之——CARMEN (.log .clf)文件转换为rosbag
If you find any loopholes later, don't tell China!
[worthy of collection] centos7 installation MySQL complete operation command
Feedforward feedback control system design (process control course design matlab/simulink)
Codeforces Round #786 (Div. 3) ABCDE
【TcaplusDB知识库】TcaplusDB机器初始化和上架介绍
NVME2.0协议——新特性
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;)
记一次 .NET 某物管后台服务 卡死分析
go-zero微服务实战系列(七、请求量这么高该如何优化)
Institute of Microbiology, Chinese Academy of Sciences recruited 20 young PI, with a resettlement fee of 2million yuan and a start-up fund of 10million yuan (long-term effective)
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)
VPT Model Video Explanation