Reputation: 1043
I'm getting this warning when trying to initialize an array to a constant size.
#2170-D use of a const variable in a constant expression is nonstandard in C
#file.h
typedef struct {
// LED Blink Pattern
.....
} LEDSeq
void addError(LEDSeq);
void runLEDErrors();
....
#file.c
const uint8_t MAXERRORS = 4;
LEDSeq errors[MAXERRORS];
uint8_t errorsLength = 0;
....
Essentially it's a bit of code that is going to loop over LED error sequences that are added during run time. I've got to use a fixed size array because realloc isn't available in my environment. The code all works. I'm just wondering why I'm getting this error.
Upvotes: 3
Views: 4084
Reputation: 145829
A const
object is not a constant in C but a read-only object. An array declared at file scope (or any array with static storage duration) has to have a constant expression as its number of elements.
This is valid:
#define MAXERRORS 4
LEDSeq errors[MAXERRORS];
Upvotes: 8