Creating an array of pointers to multi-levelled array

I'm looking for a mechanism to create an array of pointers to certain elements within a multi layered array of structs.

I currently have 2 structs defined. One for month and one for day.

struct Day
{
    uint8_t ValueA;
    uint8_t ValueB;
    uint8_t ValueC;
    uint8_t ValueD;
    uint8_t ValueE;
    uint32_t TimeA;
    uint32_t TimeB;
};

struct Month
{
    uint8_t Month;
    uint8_t Year;
    uint8_t DaysInMonth;
    uint8_t ValueA;
    Day Days[31];
};

in my code I then create the months array as follows. There are only 2 month arrays as i store current and next month.

Month Months[2];

I want to create another array that stores pointers to the next 7 days (its for weather forcast).
So for example (in sudo as I know this syntax does not work) I want to do something like this:

int * forecast[7];

forecast[0] = &Months[0].Days[5];
forecast[1] = &Months[0].Days[6];
forecast[2] = &Months[0].Days[7];
forecast[3] = &Months[0].Days[8];
forecast[4] = &Months[0].Days[9];
forecast[5] = &Months[0].Days[10];
forecast[6] = &Months[0].Days[11];

But this gives me the error:

cannot convert 'Day*' to 'int*' in initialization

Any suggestions much appreciated!

Day* forecast[7];

forecast[0] = Months[0].Days[5];
1 Like

missing an & as Months[0].Days[5] is a Day structure, not a pointer

1 Like

Thank you both!

Simply changing the int to Day solved that. Something so simple I should not of missed!! :slightly_smiling_face:

Always happy learning!

even not missed, but removed it intentionally...
Yes, it was a mistake, I forget about the pointer

if you did

Day forecast[7];
forecast[0] = Months[0].Days[5];
...

then you are making copies of the original

you need to do

Day* forecast[7];
forecast[0] = &(Months[0].Days[5]);
...

Yes that's what i did sorry i wasn't clear. I changed the int* to Day* from my originally posted code.

OK ! have fun

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.