【发布时间】:2013-12-18 09:31:18
【问题描述】:
我想用普通的C 初始化一个字符串数组,满足以下要求:
(A) 我需要头文件中的字符串,因为其他一些模块将它们用作纯字符串,所以我在头文件中声明:
extern const char* const ERRMSG_VM_0001;
extern const char* const ERRMSG_VM_0002;
extern const char* const ERRMSG_VM_0003;
extern const char* const ERRMSG_VM_0004;
在源文件中:
const char* const ERRMSG_VM_0001 = "[VM-001] some text ";
const char* const ERRMSG_VM_0002 = "[VM-002] more text ";
const char* const ERRMSG_VM_0003 = "[VM-003] other text ";
const char* const ERRMSG_VM_0004 = "[VM-003] and even more";
(B) 我还需要将这些字符串放在一个数组中,所以我在(相同的)源(如上)中进行了尝试:
static const char* error_table[4] =
{
ERRMSG_VM_0001,
ERRMSG_VM_0002,
ERRMSG_VM_0003,
ERRMSG_VM_0004
};
显然,编译器抱怨error: initializer element is not constant ...所以现在我想知道如何以纯C 方式实现这一点,而不是C++(这类似于Tricky array Initialization,但不一样) .
【问题讨论】:
标签: c arrays string initialization