假设“其他地方定义的常量”现在看起来像这样
const char *ARR1 = "Alpha";
const char *ARR2 = "Bravo";
const char *ARR3 = "Charlie";
解决方案 1
const char *ARR_Value[] = "Alpha", "Bravo", "Charlie", 0;
const char *ARR_Name[] = "ARR1", "ARR2", "ARR3", 0;
// Pseudo code
Read file keyword
find matching ARR_Name[]
apply corresponding ARR_Value[] to input[]
更优雅的解决方案
typedef struct {
const char *Name;
const char *Value;
} ARR_t;
const ARR_t ARR[] = {
{ "ARR1", "Alpha"},
{ "ARR2", "Bravo"},
{ "ARR3", "Charlie"},
{ 0, 0}
};
// Pseudo code
Read file keyword
find matching ARR[] by doing strcmp(keyword, ARR[i].Name)
apply corresponding ARR[i].Value to input[]
不清楚 OP 是否需要初始化然后更改 input[] 内容,因为帖子是char input[4][10]
而不是const char input[4][10]
。