C语言 错误:表达式必须有一个常量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25542109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:18:31  来源:igfitidea点击:

error: expression must have a constant value

carrayscompiler-errorsconst

提问by user3274807

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 valueHow 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"};

The following not work either

以下也不起作用

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

If someone could help me to understand this non constvalues ...

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

回答by Yu Hao

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

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

This is similar to:

这类似于:

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

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

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

回答by user3386109

Note that the addressesof globaland/or staticvariables areconsidered compile time constants. So if you make G_Commandsan array-of-pointers, then you can initialize the array as shown below

请注意,地址全球性和/或静态变量认为是编译时间常数。所以如果你创建G_Commands一个指针数组,那么你可以初始化数组,如下所示

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

const T_Entry EntryMemory      = { "MEM" , "Memory"       };
const T_Entry EntryPowerSupply = { "PWRS", "Power supply" };
const T_Entry EntrySystem      = { "SYS" , "System"       };

static const T_Entry *G_Commands[] =
{
    &EntryMemory,
    &EntryPowerSupply,
    &EntrySystem,
    NULL
};

static const T_Entry *G_Menus[] =
{
    &EntryPowerSupply,
    NULL
};

int main( void )
{
    const T_Entry **entry, *command, *menu;

    printf( "Commands:\n" );
    for ( entry = G_Commands; *entry != NULL; entry++ )
    {
        command = *entry;
        printf( "   %-4s %s\n", command->iName, command->iComment );
    }

    printf( "\nMenus:\n" );
    for ( entry = G_Menus; *entry != NULL; entry++ )
    {
        menu = *entry;
        printf( "   %-4s %s\n", menu->iName, menu->iComment );
    }
}

回答by user694733

Unfortunately constant variables cannot be used in constant expressions. They are considered to be variables that cannot change, but not constants that can be determined at compile time.

不幸的是,常量变量不能用于常量表达式。它们被认为是不能改变的变量,而不是可以在编译时确定的常量。

Workaround:

解决方法:

#define MENU_PWRSPLY_NAME    "PWRS"
#define MENU_PWRSPLY_COMMENT "Power supply"

const T_Entry Menu_PowerSupply = { MENU_PWRSPLY_NAME, MENU_PWRSPLY_COMMENT };

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