简体   繁体   中英

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?

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 .

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 var1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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