在 C 中同一字段的另一个字段中使用结构字段

Using struct field in another field of the same field in C

我是 C 编程的初学者。你能告诉我为什么我不能在另一个字段中使用结构中的一个字段吗?

struct example{
    int length;
    int array[length];
}

你能告诉我结构的作用域是什么吗?我知道这只是结构标签而不是真正的结构变量,但请向我解释。

length 不是一个变量,它是一个结构成员。它只能与包含该结构类型的变量一起使用,例如

struct example myvar;
myvar.length = 10;

由于在声明变量时必须知道数组成员的大小,因此它不可能依赖于同一结构的另一个成员的值。

如果数组的大小不同,您可以使用灵活的数组成员。参见 What's the need of array with zero elements?