【发布时间】:2017-06-04 08:25:19
【问题描述】:
是否可以在初始化程序中使用三元运算符初始化静态数组? 例如:
// Array of functions
static const Callback_t CallbackArray[] =
{
GenConfig,
GenInfo,
/* here is the ternary operator to chose a callback */
Test() ? Config : NULLConfig,
};
其中 Test() 是一个返回 0 或 1 的函数
// somewhere else in the code
int gVar = 0;
int TEST(void)
{
return gVar;
}
【问题讨论】:
-
是的。这是完全有效的。只要
Test()是一个宏 -
您尝试过吗?它奏效了吗?如果没有,您遇到了什么错误?
-
我认为这里唯一的问题是
Test(),因为函数不是常量表达式。如果不是这样,代码应该没问题。 -
Test() 是一个函数,正如我所写的,而不是宏。它似乎有效,但我不明白如何,因为 Test() 检查全局变量
-
在初始化中,代码使用
Test()。有一个函数叫做TEST()。TEST()与Test()不同。发布 true 代码。目前还不清楚要问什么。
标签: c arrays initialization ternary-operator constant-expression