Gopi
Gopi

Reputation: 19864

Integer array of variable length

I have a

int val,i;
scanf("%d",&val);
int a[] = { (val & 0x7) , (val & 0x5) };
for(i=0;i<2;i++)
printf("%d\n",a[i]);

I am confused whether the above code is good or not? I get this error on some compiler

expression must have a constant value

but on another this works fine.So is this code good?

Upvotes: 2

Views: 41

Answers (1)

haccks
haccks

Reputation: 106012

Variables in initializer list is not allowed before C99.

C89: 6.5.7:

All the expression in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.

From C99, it is allowed. Make sure you are compiling with -std=c99 flag.

Upvotes: 4

Related Questions