"variable-size type declared" error for const sized array

Ian Lance Taylor ian@airs.com
Thu Sep 1 18:22:00 GMT 2005


Bahadir Balban <krupiyer@gmail.com> writes:

> I see that it works when the array is a local variable. But in the
> following piece of code:
> 
> #include <stdio.h>
> 
> // const int const_asize = 10;
> #define  define_asize = 10;
> 
> int array[define_asize] = {1,2,3,4,5,6,7,8,9,0};
> 
> int main(int argc, char * argv[])
> {
> 
>     return 0;
> }
> 
> 
> With a define it compiles with no errors, however if you replace it
> with the constant variable, I get the following error:
> 
> "error: variable-size type declared outside of any function"
> "error: variable-sized object may not be initialized"
> 
> This is on gcc version 3.3.5 compiling with the following:
> 
> gcc -o consttest main.c
> 
> adding a -std=gnu99 does not make any difference. 
> 
> My opinion is that it is not the best behaviour to differentiate
> between a literal and a const int when they're declared global, but
> make no distinction when they're local. What do you think?

This behaviour is specified by the C standard.  The C standard spells
out what precisely may be used when declaring the size of an array.
Const variables are not permitted.

Obviously it would be possible for gcc to extend the C standard here,
but such extensions are generally only supported when there is a real
use for them.

Ian



More information about the Gcc-help mailing list