0
 unsigned char rtc_time[6] = { pThis->hoursTens, pThis->hoursUnits, pThis->minutesTens, pThis->minutesUnits, pThis->secondsTens, pThis->secondsUnits };

Does not compile. I receieve the error (6 times): constant expression required

Each of the variables are declared as an unsigned char. I have tried casting to (const) with no luck.

This is in MPLAB X IDE, C language, using Hi-Tech-PICC compiler v9.65PL1.

What is the problem?

It works when I define the variable as below, but I need to use the variables above.

 unsigned char rtc_time[6] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 };
Michael
  • 309
  • 2
  • 3
  • 16

2 Answers2

8

You cannot initialize an array with values whose values are unknown at compile-time. The values of your struct are unknown at compile-time and so are not const expressions.
Whereas 0x1, 0x2, ... are const expressions that can be evaluated at compile-time.

You might declare an array and then set the values during runtime, like

unsigned char rtc_time[6];
...
rtc_time[0] = pThis->hoursTens;
//go on
bash.d
  • 13,029
  • 3
  • 29
  • 42
3

When expressions consist of calculated values you must execute them as statements and not initializors.

...
unsigned char rtc_time[6];
...
rtc_time[0] = pThis->hoursTens;
rtc_time[1] = pThis->hoursUnits;
. . .
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329