简体   繁体   English

C在结构中为结构声明一个数组

[英]C declare an array for a struct inside a struct

So I have something like this:所以我有这样的事情:

struct cat{
    int breed;
    enum state status;
    ...    

    struct{
        int catNumber;
        ...
    } feederOperations[MAX_FEEDER];
}cats[MAX_CATS];

Don't worry if the code deosn't make sense, all I'm wondering is how should/ can I declare a new 1d array of the inside structure?如果代码 deosn 没有意义,请不要担心,我想知道的是我应该/如何声明内部结构的新一维数组?

Once you remove the anonymous of the inner structure and give it a name.一旦您删除内部结构的匿名并为其命名。

There is no special scoping rules for inner structures in C which means that the scope of struct cat is the same as the scope of struct feederOp . C 中的内部结构没有特殊的范围规则,这意味着struct cat的范围与struct feederOp的范围相同。

struct cat{
    int breed;
    enum state status;
    ...    

    struct feederOp {
        int catNumber;
        ...
    } feederOperations[MAX_FEEDER];
}cats[MAX_CATS];

Thus you can create variable of type struct feederOp wherever you need as below.因此,您可以在任何需要的地方创建struct feederOp类型的变量,如下所示。

struct feederOp var1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM