質問

 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 };
役に立ちましたか?

解決

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

他のヒント

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;
. . .
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top