Home > Software design >  Initialize an array of an array of structs
Initialize an array of an array of structs

Time:12-09

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},
    }
};

CodePudding user response:

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},
    }
};
  • Related