错误:表达式必须具有恒定值 [英] error: expression must have a constant value

查看:70
本文介绍了错误:表达式必须具有恒定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来查找如何解决此错误.

I need some help to find how to resolve this error.

typedef struct {
    const char *iName;
    const char *iComment;
} T_Entry;

const T_Entry Menu_PowerSupply = { "PWRS", "Power supply"};

static const T_Entry G_Commands[] = {
    { "MEM", "Memory"},
    {Menu_PowerSupply.iName,Menu_PowerSupply.iComment},
    { "SYS", "System"}
};

我收到错误消息:表达式必须具有恒定值 我该如何解决?

I got the error : expression must have a constant value How can I solve this ?

对我来说,链接时间是已知的,并且在固定地址处具有固定值:我错了吗

For me at link time is known and at a fixed address with a fixed value : Am I wrong

我的目的是将以下代码放入库中

My purpose is to put the following code into a library

const T_Entry Menu_PowerSupply = { "PWRS", "Power supply"};

以下内容也不起作用

static const T_Entry G_Commands[] = {
    { "MEM", "Memory"},
    Menu_PowerSupply,
    { "SYS", "System"}
};

如果有人可以帮助我理解此 非const 值...

If someone could help me to understand this non const values ...

推荐答案

该错误是因为全局变量的初始化程序必须是常量表达式,但是即使Menu_PowerSupply被定义为const,它也不是常量表达式.

The error is because the initializer for global variables must be constant expression, but even though Menu_PowerSupply is defined as const, it's not a constant expression.

这类似于:

const int n = 42;
int arr[n]; 

不能在C89中编译,因为n不是常量表达式. (它仅在C99中才编译,因为C99支持VLA)

doesn't compile in C89 because n is not a constant expression. (it does compile in C99 only because C99 supports VLA)

这篇关于错误:表达式必须具有恒定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆