Search code examples
cvariablesembeddedmplab

constant expression required


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

Solution

  • 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