gpt4 book ai didi

c - 如何在 C 中初始化 Const 结构 - 以避免 QAC 警告

转载 作者:行者123 更新时间:2023-11-30 15:03:06 25 4
gpt4 key购买 nike

我在初始化 const 结构时收到以下 QAC 警告。

"[C] Initializer for 'struct', 'union' or array type, or any object with static storage duration, must be a constant "

FPARM1C2.c

 const PROFI_tstBlock FPARM_astFblBlockTable[] = {FPARM_ast_FblBlock_Table};

const PROFI_tstPartition FPARM_astFblPartitionTable[] = {FPARM_ast_FblPartition_Table};

上述声明导致 QAC 警告。

FPARM1CA.H

#define FPARM_ast_FblBlock_Table \
0x00000000 , 0x00000000 , 0x00000000 , 0x00000000 , 0x00020000 , 0x00000000 , 0x00001000 , 0x00000000 , FPARM_nMemTypeIntFlash1 , 0x01 , 0x0000 , FPARM_bi8DataBlock | FPARM_bi8CommonBlock , 0x0000

#define FPARM_ast_FblPartition_Table \
(uint32)__ghsbegin_FLoaderIdent, (uint32)0x0, (uint32)FPARM_astFblBlockTable, (uint32) 0x0, 1, 0, 0x00, 0x00, 0x00, 0x00

Profi1c1.h

typedef struct PROFI_tstPartition
{
uint32 xAddressID;
uint32 u32Reserved1;
uint32 xBlockTableAdr;
uint32 u32Reserved2;
uint16 u16NumberOfBlocks;
uint16 u16GlobalProperties;
uint8 au8Reserved[4];
} PROFI_tstPartition;


typedef struct PROFI_tstBlock
{
uint32 xPhysicalAddress;
uint32 u32Reserved1;
uint32 xLogicalAddress;
uint32 u32Reserved2;
uint32 xBlockLength;
uint32 u32Reserved3;
uint32 xSectorSize;
uint32 u32Reserved4;
uint8 u8BlockMemoryType;
uint8 u8Security;
uint16 u16Reserved;
uint16 u16BlockProperties;
uint16 u16Reserved2;
} PROFI_tstBlock;

是否有任何类型转换或更好的方法来初始化 const 结构以避免警告?

最佳答案

任何具有静态存储持续时间的变量都无法从另一个变量进行初始化。因此,如果它是一个数组,它就不能包含对初始化列表中其他变量的任何引用。

不要做这样的事情:

const type1 array1 [] = {1, 2, 3};
const type2 array2 [] = {0, array1, 0}; // won't work

而是做这样的事情:

#define ARRAY1_INIT { 1, 2, 3 }

const type1 array1 [] = ARRAY1_INIT;
const type2 array2 [] = {0, ARRAY1_INIT, 0};

关于c - 如何在 C 中初始化 Const 结构 - 以避免 QAC 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40836564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com