2

I am trying to initialize an array of an array of structs in C. Is this possible?

Example to illustrate the issue (does not compile):

typedef struct myStruct
{
    int row;
    int cols;
} myStruct;

myStruct dataSets[][] =
{
    myStruct dataSet1[]
    {
        {0,1},
        {0,2},
        {0,3},
    },

    myStruct dataSet2[]
    {
        {1,1},
        {1,2},
        {1,3},
    }
};
1
  • your myStruct definition is entirely different than what you are initializing in it's instance. Commented Dec 8, 2021 at 14:30

1 Answer 1

6

When initializing a multidimensional array, only the outer array dimension may be omitted. You also don't need to specify the type for nested initializers as the type is implied from context:

myStruct dataSets[][3] =
{
    {
        {0,1},
        {0,2},
        {0,3},
    },
    {
        {1,1},
        {1,2},
        {1,3},
    }
};
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.