给定地址处的结构体静态声明的灵活数组不起作用?

问题描述 投票:0回答:1
typedef struct {
uint64_t register;
uint32_t size;
} element_t;

typedef struct {
uint64_t idx;
element_t element[]; // Flexible array
} frame_t

我想在我为此结构保留的已定义内存位置静态声明

frame_t *frame = (frame_t *) 0xdeadbeef; /* this is the predefined memory location */
*frame = {
{.idx = 0x1;
 .element = {
   [0] = {
     .register = 0xdad;
     .size = 0x10;
   },
   [1] = { .register =0xcad;
     .size = 0x20;
   }
 }
},
{ .idx = 0x2;
  .element = {    
   [0] = { 
     .register = 0xdead;
     .size = 0x10;
   }
}
}
};

为什么上面的灵活数组静态声明不起作用,声明此结构的正确方法是什么?

c embedded
1个回答
0
投票

这里有很多问题

  1. 您不能拥有包含灵活内存数组的结构数组。
  2. 分配具有灵活数组成员的结构不会复制此成员。
  3. 不能像初始化时一样进行赋值。为此,您需要使用复合文字。但它也不会复制灵活数组。
© www.soinside.com 2019 - 2024. All rights reserved.