首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将Bit字段在C中初始化为全局

无法将Bit字段在C中初始化为全局
EN

Stack Overflow用户
提问于 2016-04-25 10:19:08
回答 2查看 696关注 0票数 0

我已经声明了一个位字段,如下所示。

代码语言:javascript
复制
struct  {
    volatile uint8_t screenflag:1;
    volatile uint8_t logoflag:1;
    volatile uint8_t oledflag:1;
    volatile uint8_t animationflag:1;
    volatile uint8_t clockdialflag:1;
    volatile uint8_t update_screen:1;
    volatile uint8_t BLE_activity:1;
    uint8_t ble_status:1;
} oled_flag;

但是,当我尝试用Main()函数初始化Bitfield的元素时,它在编译时显示了以下错误。

....\Src\main.c(95):警告:#77-D:此声明没有存储类或类型说明符oled_flag.screenflag=1;....\Src\main.c(95):错误:#147:声明与"struct oled_flag“不兼容(在第92行声明) oled_flag.screenflag=1;....\Src\main.c(95):错误:#65:预期为";“oled_flag.screenflag=1;....\Src\main.c(96):警告:#77-D:此声明没有存储类或类型说明符 oled_flag.logoflag=0;....\Src\main.c(96):错误:#147:声明与"struct oled_flag“(在第95行声明)oled_flag.logoflag=0不兼容;....\Src\main.c(96):错误:#65:预期a;”oled_flag.logoflag=0;....\Src\main.c(97):警告: 77-D:此声明没有存储类或类型说明符oled_flag.oledflag=1;....\Src\main.c(97):错误:#147:声明 与"struct oled_flag“(在第96行声明)不兼容;....\Src\main.c(97):错误:#65:预期a;”oled_flag.oledflag=1;....\Src\main.c(98):警告: 77-D:此声明没有存储类或类型说明符oled_flag.animationflag=0;....\Src\main.c(98):错误:#147: 声明与"struct oled_flag“(在第97行声明)oled_flag.animationflag=0不兼容;....\Src\main.c(98):错误:#65:预期a; oled_flag.animationflag=0;....\Src\main.c(99):警告:#77-D:此声明没有存储类或类型说明符 oled_flag.clockdialflag=1;....\Src\main.c(99):错误:#147:声明与"struct oled_flag“(在第98行声明)oled_flag.clockdialflag=1不兼容;....\Src\main.c(99):错误:#65:预期a; oled_flag.clockdialflag=1;....\Src\main.c(100):警告:#77-D:此声明没有存储类或类型说明符

等等。

初始化代码是:

代码语言:javascript
复制
oled_flag.screenflag=1;
oled_flag.logoflag=0;
oled_flag.oledflag=1;
oled_flag.animationflag=0;
oled_flag.clockdialflag=1;
oled_flag.update_screen=0;
oled_flag.BLE_activity=0;
oled_flag.ble_status=1;

但是,当我在Main()函数中初始化位字段的元素时,它工作得很好。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-25 10:29:17

在C中,不能有函数之外的语句。您需要将语句移动到函数中,或者将全局变量与其声明一起初始化:

代码语言:javascript
复制
struct  {
    volatile uint8_t screenflag:1;
    volatile uint8_t logoflag:1;
    volatile uint8_t oledflag:1;
    volatile uint8_t animationflag:1;
    volatile uint8_t clockdialflag:1;
    volatile uint8_t update_screen:1;
    volatile uint8_t BLE_activity:1;
    uint8_t ble_status:1;
} oled_flag = {
    .screenflag = 1,
    .logoflag = 0,
    .oledflag = 1,
    .animationflag = 0,
    .clockdialflag = 1,
    .update_screen = 0,
    .BLE_activity = 0,
    .ble_status = 1
};
票数 3
EN

Stack Overflow用户

发布于 2019-02-15 21:03:34

另一种方法是将位字段结构放到这样的联合中:

代码语言:javascript
复制
union {
  volatile uint8_t byte;
  struct {
     volatile uint8_t screenflag:1;
     volatile uint8_t logoflag:1;
     volatile uint8_t oledflag:1;
     volatile uint8_t animationflag:1;
     volatile uint8_t clockdialflag:1;
     volatile uint8_t update_screen:1;
     volatile uint8_t BLE_activity:1;
     uint8_t ble_status:1;
   }bit_field
}oled_flag;

现在,使用联合,您可以使用oled_flag.byte = {0x95}手动初始化主函数之外的值。您仍然可以使用oled_flag.bit_field.screenflag = ...访问您的位。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36837769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档