4

在 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}}
}
4

1 回答 1

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}}
    };
于 2013-04-13T16:19:40.667 回答