在C中,我需要静态预分配一个数字数组,每个数字与不同的字符串数组相关联。像下面这样的代码可以解决这个问题:
struct number_and_strings {
int nnn;
char **sss;
}
static struct number_and_strings my_list[] = {
{12, {"apple","banana","peach","apricot","orange",NULL}},
{34, {"tomato","cucumber",NULL}},
{5, {"bread","butter","cheese",NULL}},
{79, {"water",NULL}}
}
答案 0 :(得分:5)
sss
是指向指针的指针。因此无法将指针数组直接分配给它。您可以使用复合文字(这是C99功能)分配如下:
static struct number_and_strings my_list[] = {
{12, (char*[]){"apple","banana","peach","apricot","orange",NULL}},
{34, (char*[]){"tomato","cucumber",NULL}},
{5, (char*[]){"bread","butter","cheese",NULL}},
{79, (char*[]){"water",NULL}}
};