0

I'm having some syntax troubles with my code. A bit of context... My program has a schedule, it's an array of 24 bytes. There's one schedule per day, so 7 arrays.

I want to have a single array of 7 elements storing references to the above 7 arrays. This way, by calling schedules[1], I get schedule1[24], which is Monday.

// One schedule per day (0 = sunday)
byte schedule0[24];
byte schedule1[24];
byte schedule2[24];
byte schedule3[24];
byte schedule4[24];
byte schedule5[24];
byte schedule6[24];
byte * schedules[7] = {&schedule0, &schedule1};

The problem comes from the last line, the error being "a value of type "byte (*)[24]" cannot be used to initialize an entity of type "byte *" ".

I tried inserting [24] before or after the star/pointer character, with no luck.

Could any of you please show me the correct syntax to do this?

1
  • 1
    What about byte* schedules[2] = {schedule0, schedule1};? Commented Feb 11, 2019 at 10:55

3 Answers 3

1

In C++, name of the array is the pointer to the first element in the array. So in your case schedule0 is a pointer to &schedule0[0] not &schedule0.

You can change the last line as

byte * schedules[2] = {&schedule0[0], &schedule1[0]}; or byte * schedules[2] = {schedule0, schedule1};

This will create a pointer array containing the base address of the scheduleX arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

I want to have a single array of 7 elements storing references to the above 7 arrays. This way, by calling schedules[1], I get schedule1[24], which is Monday.

A different approach is needed to index across days. This can be done using a two dimensional array and some pointer arithmetic.

Declare a two dimensional array for your schedules:

byte schedulesArray[7][24];

Declare pointers that point to the schedules for each day:

byte *schedule0 = &schedulesArray[0][0];
byte *schedule1 = &schedulesArray[1][0];
byte *schedule2 = &schedulesArray[2][0];
byte *schedule3 = &schedulesArray[3][0];
byte *schedule4 = &schedulesArray[4][0];
byte *schedule5 = &schedulesArray[5][0];
byte *schedule6 = &schedulesArray[6][0];

Declare a pointer to the first element of the two dimensional array:

byte *schedules = &schedulesArray[0][0];

So if we seed some data:

schedule0[0] = 1;

schedule1[0] = 11;
schedule1[1] = 12;

schedule2[0] = 21;
schedule2[1] = 22;

Then you can use the schedules pointer to index across days:

schedules[0];   // = 1
schedules[24];  // = 11
schedules[25];  // = 12
schedules[48];  // = 21
schedules[49];  // = 22

This works because multidimensional arrays are laid out as a contiguous block of memory.

Comments

0

Simply, you can't do it.

And here is explanation why:

Basically, a reference is an alias to an existing variable. This means, if you apply any operation on a reference, it will behave as if you were using the original variable name. And there are no references at a reference or pointer at references, references don't allocate any memory so there is nothing that you can use to put in an array.

What you can do is create an array of pointers and it will work.

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.