user3817250
user3817250

Reputation: 1043

c warning: use of const variable in a constant expression is non-standard in C

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

Answers (1)

ouah
ouah

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

Related Questions