【问题标题】:How can an array of structs be initialized using a const struct in global scope?如何在全局范围内使用 const 结构初始化结构数组?
【发布时间】:2018-01-16 13:21:43
【问题描述】:

我想使用类似于以下的代码(但要复杂得多 - 这是一个简化的示例)来初始化结构数组,但在编译期间我收到错误“表达式必须具有常量值”。

typedef struct
{
    int x;
    int y;
} windowStruct_t;

static const windowStruct_t windowStructInit =
{
    .x = 3,
    .y = 5,
};

// These get defined differently at times.  This is simplified for the example.
#define NUM_ARRAY_ELEMENTS (2)
#define REPEAT_NUM_ARRAY_ELEMENTS_TIMES(x) (x),(x)

// The following line causes the error "expression must have a constant value" twice.
windowStruct_t windowStruct[ NUM_ARRAY_ELEMENTS ] = 
    { REPEAT_NUM_ARRAY_ELEMENTS_TIMES( windowStructInit ) };

void someFunction( void )
{
    volatile int x = windowStruct[0].x;
}

void anotherFunction( void )
{
    volatile int y = windowStruct[1].y;
}

手动扩展宏并将导致错误的行替换为以下内容会得到相同的结果:

windowStruct_t windowStruct[ NUM_ARRAY_ELEMENTS ] = 
    { windowStructInit, windowStructInit };

但这编译没有错误:

windowStruct_t windowStruct[ NUM_ARRAY_ELEMENTS ] =
    { { .x = 3, .y = 5 }, { .x = 3, .y = 5 } };

如果我将数组声明移到函数范围内,它编译时不会出错(我忽略了 someFunction() 和 anotherFunction() 现在访问不同的数组并且它们的生命周期不同的事实):

void someFunction( void )
{
    windowStruct_t windowStruct[ NUM_ARRAY_ELEMENTS ] = 
        { REPEAT_NUM_ARRAY_ELEMENTS_TIMES( windowStructInit ) };

    volatile int x = windowStruct[0].x;
}

void anotherFunction( void )
{
    windowStruct_t windowStruct[ NUM_ARRAY_ELEMENTS ] = 
        { REPEAT_NUM_ARRAY_ELEMENTS_TIMES( windowStructInit ) };

    volatile int y = windowStruct[1].y;
}

将数组声明留在函数范围内,如果它们被声明为“静态”,则会返回错误消息:

void someFunction( void )
{
    static windowStruct_t windowStruct[ NUM_ARRAY_ELEMENTS ] = 
        { REPEAT_NUM_ARRAY_ELEMENTS_TIMES( windowStructInit ) };

    volatile int x = windowStruct[0].x;
}

因此,当数组被声明为自动变量(在堆栈上)时,它们似乎可以在内存分配是静态的情况下以一种不允许的方式初始化(​​无论是在函数范围内还是在全局范围内)即使没有“静态”关键字,分配也是静态的)。有没有办法像原始示例一样使用 const 结构在全局范围内初始化数组?

我使用的是 C,而不是 C++。我不想使用动态内存分配。该编译器是 TI 的 ARM 编译器 V16.6.0.STS,包含在其 Code Composer Studio 环境中。

【问题讨论】:

  • 尝试将windowStruct_t windowStruct[ NUM_ARRAY_ELEMENTS ] = ...定义为static const windowStruct_t windowStruct[ NUM_ARRAY_ELEMENTS ] = ...,看看会发生什么。
  • 当它在全局范围内时,我得到与“static”、“const”、“static const”或两者都没有的相同编译错误。
  • @MadPhysicist 别再猜测了,这是 C,而不是 C++。
  • @SteveStrobel const structs 不是编译时常量。并且“即使没有 static 关键字”是因为您将 static 存储类与 静态存储持续时间 :) 混淆了
  • 重复初始化是正确的,现在写一个关于它的答案并自我接受。另一个根本做不到。

标签: c arrays struct


【解决方案1】:

const 对象不是 C 常量。而是使用非自动存储对象所需的 constants

定义一个初始化器{ .x = 3, .y = 5 }

typedef struct windowStruct_s {
  int x;
  int y;
} windowStruct_t;

#define windowStruct_t_default_initializer { .x = 3, .y = 5 }
#define NUM_ARRAY_ELEMENTS (2)
#define REPEAT_NUM_ARRAY_ELEMENTS_TIMES(x) x, x /* no () */

windowStruct_t windowStruct[NUM_ARRAY_ELEMENTS] = {
    REPEAT_NUM_ARRAY_ELEMENTS_TIMES(windowStruct_t_default_initializer) };

int someFunction(void) {
  volatile int x = windowStruct[0].x;
  return x;
}

int anotherFunction(void) {
  volatile int y = windowStruct[1].y;
  return y;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多