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。您不能在编译时用另一个(全局)变量初始化全局变量。编译器不会将其视为常量。
推荐阅读
- javascript - Axios + Electron.js 错误:`xhr.js:120 Refused to set unsafe header "user-agent"`
- symfony4 - Symfony 4:将供应商捆绑为“自己的捆绑”以轻松开发?
- java - 同一实体错误的多种表示
- c# - 不确定如何正确地将数据提取到包含字符串、int 和 double 属性的类列表中
- python - 如何从烧瓶启动和停止子进程
- php - 外键约束的格式不正确。你能帮助我吗?
- c# - 如何使用正文表单数据在 Xamarin C# url 中获取数据
- azure - 在 Powershell 中获取 Azure HybridWorker 环境变量
- gis - 拆分资产:管理资产 ID 和相关记录的选项
- c# - 线程问题 - 阻塞 UI 线程