c - 错误 "expression must have a constant value"

标签 c

当我使用 const 变量“a”和“b”来初始化结构化变量“cfg”时,我面临以下编译器问题“表达式必须具有常量值”

static const unsigned int a = 1;
static const unsigned int b = 2;


typedef struct
{
    const uint32 InitTypestruct_elem1;   
    const uint32 InitTypestruct_elem2;   
}InitType;

typedef struct
{
    InitType BoardTypestruct_elem1;
}BoardType;


static const BoardType cfg =
{
    /* pbgc cfg */
    .BoardTypestruct_elem1 =
    {
        ***.InitTypestruct_elem1 = a,
        ***.InitTypestruct_elem2 = b
    }
};


const InitType *cfg(void)
{
    return &cfg.BoardTypestruct_elem1;
}


int main()
{
    //cfg

    return 0;
}

编译警告原因在***行

为什么会出现这个问题? a 和 b 是常量值?

最佳答案

简化其他答案:

static const unsigned int a = 1;

a 是一个变量,不管它是const。您不能在编译时使用另一个(全局)变量初始化全局变量。编译器不会将其视为常量。

关于c - 错误 "expression must have a constant value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57572623/

相关文章:

c - x264 配置链接和构建问题

c - DevC++ C 调用 OpenCobolIDE 模块

c - PyFile_Type 替换为 ..?

c - 在 ansi c 中读取和格式化文本文件

c - Arduino C - 无法创建新方法和数组

c - 使用宏 STATIC 的重要性

c - 打印链表导致无限循环(C)?

python - 如何在 C 语言中使用经过训练的图形文件

c - 避免在 Solaris 操作系统上重写文件

c - Msgrcv 未收到消息,但消息已正确发送