2

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?

3
  • hmmm? You just did. Commented Sep 9, 2020 at 4:00
  • I mean if I need a separate one in another function? Also isn't that sort of like a 2d array so it's tied to cats[]? or can I call for feederOperations[] just treating it like a normal 1d array? @ikegami Commented Sep 9, 2020 at 4:03
  • @PapaSheng You can declare as many as you like, using the same syntax you're using for feederOperations or any other data type you need. Commented Sep 9, 2020 at 4:15

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.