C 错误 X 不是指针;您是要使用“.”吗?

C error X is not a pointer; did you mean to use '.'

我正在学习一些 C,我有以下代码,基本上我想做的是在 for 循环中增加 people 数组的大小,但目前我收到一个错误。 您能否通过简短的解释为我的脚本提供修复?

#include <stdio.h>

struct Person {
   int id;
};

struct State {
   struct Person people[0];
};

int main() {
    printf("Hello, world!");

    struct State state = {};

    for(int i = 0; i < 10; ++i) {
      struct Person person = {};
      person.id = i;
      state->people[i] = person;
    }
  
    return 0;

我收到错误:

 clang-7 -pthread -lm -o main main.c
main.c:19:12: error: member reference type 'struct State' is not
      a pointer; did you mean to use '.'?
      state->people[i] = person;
      ~~~~~^~
           .
1 error generated.
exit status 1

如果您知道 -> 的用途,则错误很严重 self-explanatory:当结构(其左操作数)是指针时使用它。它相当于 (*pointer_to_struct).-> 的右操作数得到什么类型并不重要。

您的代码中没有指向结构的指针,因此您不能使用 ->

另外struct State state = {}是无效的C,初始化列表不能为空。您至少需要设置一个元素,例如,您可以执行 {0} 并将结构中的所有内容设置为 zero/null.