Elmi
Elmi

Reputation: 6193

const-type is not accepted as constant initializer

following (simplified) piece of code causes an

error C2099: initializer is not a constant

in last line of this snippet:

static const char rowmans_1_width = 16;
...
const char rowmans_width[96] = {rowmans_1_width, rowmans_2_width, rowmans_3_width,...

So rowmans_1_width is defined as "const" but compiler still complains it is not constant. This happens when I compile this as C-code, when using as C++ it works smoothly.

My Problem: I have to use a C compiler now and I can't simply use a

#define rowmans_1_width 16

because I had to change very much at very different positions - means a simple search/replace would not work. So, any ideas how I can overcome this problem?

Thanks!

Upvotes: 1

Views: 44

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

That's right, unlike C++, const variables in C are not constant expressions.

I don't think there is a portable way other than using the preprocessor:

#define rowmans_1_width 16

Upvotes: 4

Related Questions