c - 用 C 中的值填充 union 数组

标签 c

我有以下内容

/* Size 16 bytes */
typedef struct __attribute__((packed)) {

    uint16_t v1;    // 2
    uint16_t v2;    // 2
    uint16_t v3;    // 2
    uint16_t v4;    // 2

    uint8_t rsvd[6];

    uint16_t crc;   // 2

} heCell_t;

typedef struct __attribute__((packed)) {

    heCell_t c0;
    heCell_t c1;
    heCell_t c2;
    heCell_t c3;

} hePag_t;

typedef union {
    hePag_t Page[32];
    heCell_t Cell[128];
} heData_t;

由于 gcc 警告: “为 .rodata.$Flash3 设置不正确的部分属性”

对于这一行

const heData_t heData __RODATA(Flash3);

我必须用一些东西来初始化 heData,这对我来说很好,只要所有值都为 0xFF(默认闪存已删除)

const heData_t heData __RODATA(Flash3) = { 0xFF };

但是有一些警告

(near initialization for 'heData.Page[0]') [-Wmissing-braces]   

最佳答案

这将清除警告:

const heData_t heData  __RODATA(Flash3)={.Page={{{0xff}}}};

不过只有第一个字节会是0xff,其余都是0x00。要解决这个问题,您可以尝试(使用 gcc):

typedef union {
    hePag_t Page[32];
    heCell_t Cell[128];
    uint8_t bytes[128*sizeof(heCell_t)];
} heData_t

...

const heData_t heData __RODATA(Flash3)={.bytes[0 ... 128*sizeof(heCell_t)-1]=0xff};

这将用 0xff 初始化所有数据。

A reference

关于c - 用 C 中的值填充 union 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38209275/

相关文章:

在 Windows 上使用线程的 C Web 服务器

c - GtkCell渲染器文本 : "editable-set" set to FALSE does not deactivate editability?

c - C if 语句条件中 & 符号的使用

复制结构体数组元素不起作用? C

c - Valgrind: stdio.h 的函数 "puts"分配内存

C: OpenSSL RSA_private_decrypt() 失败,返回 "error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error”

c - 无法将 char 指针分配给循环中的结构

c - C 程序错误 ‘struct client_thread *’ 但参数类型为 ‘int’

c - 搜索/删除时出现 2-3 棵树分割错误

php - C 程序中的 block 系统命令