【问题标题】:How to Initialize a Const Structure in C - To Avoid QAC warnings [closed]如何在 C 中初始化 Const 结构 - 避免 QAC 警告 [关闭]
【发布时间】:2016-11-28 02:59:53
【问题描述】:

我在初始化 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 结构以避免警告?

【问题讨论】:

  • 用值FPARM_ast_FblPartition_Table(区别:两个下划线)初始化一个名为FPARM_astFblPartitionTable 的数组是为了混淆。 __ghsbegin_FLoaderIdent 是什么? (请注意,以两个下划线开头的名称是为实现保留的;最好来自其他人的代码。)FPARM_astFblBlockTable 是什么?它很可能是导致问题的那两个之一 - 您可以通过将值替换为 0 并看到警告消失来验证这一点。
  • 感谢 jonathan 的建议,但这是我继承的遗留代码,并试图修复软件版本的警告 :)
  • “更改为 0”的建议仅用于诊断是否是这些名称中的一个或两个导致了问题。将1改为0;编译。将另一个改为0;编译。如果您不展示 txAddressFormat 是什么,以及 __ghsbegin_FLoaderIdentFPARM_astFblBlockTable 是什么,我们将无能为力。
  • 这两个变量都会导致警告 - 我删除了两个变量并将其替换为 0x0 - 这解决了 QAC 警告。现在我应该找到一种方法在使用它之前使变量变为 const 。再次感谢乔纳森的建议
  • 你应该使用&__ghsbegin_FLoaderIdent吗?当值不是时,地址将是常量。同样,对于FPARM_astFblBlockTable,也许但不太自信——看起来它可能是一个数组。你还没有给我们一个 MCVE (minimal reproducible example),这意味着任何试图帮助的人都是半盲的。

标签: c struct constants warnings


【解决方案1】:

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

不要这样做:

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};

【讨论】:

  • 是的,这正是我的代码中发生的事情,第二个分区数组由块数组初始化(由#define 预处理器指令初始化)
猜你喜欢
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 2012-03-30
  • 2013-01-17
  • 1970-01-01
相关资源
最近更新 更多