我正在寻找 C 中的数据结构,它允许我使用压缩语法(如下所示)声明和初始化数百个字符串,这在 C11 中是否可行?
#include <stdio.h>
enum {ENGLISH, SPANISH, FRENCH, NUM_LANGUAGES};
struct language_string =
{
const char* language_hello[NUM_LANGUAGES] = {"Hello", "Hola", "Bonjour"};
const char* language_goodbye[NUM_LANGUAGES] = {"Goodbye", "Adiós", "Au revoir"};
};
void foo(void)
{
printf(language_string.language_hello[ENGLISH]); // print "Hello"
}
编辑:我已经意识到在头文件中我可以公开所有字符串,而无需将它们包装在结构中或将它们外部化,并实现主要意图,每个字符串一行:
const char* language_hello[NUM_LANGUAGES] = {"Hello", "Hola", "Bonjour"};
const char* language_goodbye[NUM_LANGUAGES] = {"Goodbye", "Adiós", "Au revoir"};