Sheng
Sheng

Reputation: 75

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?

Upvotes: 2

Views: 53

Answers (1)

kiran Biradar
kiran Biradar

Reputation: 12732

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;

Upvotes: 1

Related Questions