Var length array's vs. const int

Gordon Sadler gbsadler1@lcisp.com
Sat Aug 11 10:54:00 GMT 2001


I seem to recall experiencing this before. As I didn't have a copy of
the standard to reference, I assumed I was wrong and used a work-around.

compiles:
enum {MBUF = 100};
char[MBUF]

compiles:
#define MBUF 100
char[MBUF]

error: (variable-size type declared outside of any function)
const int MBUF = 100;
char[MBUF]

I agree that iff char[MBUF] was variable length, then it must be
declared with block or function scope. However, const int MBUF is most
assuredly constant as evidenced by trying:

const int MBUF = 100;
int MBUF = 5;

gcc -c 
2: redefinition of `MBUF'
1: `MBUF' previously defined here

The C99 standard (6.7.5.2:4):
[#4] If the size is  not  present,  the  array  type  is  an
incomplete  type.   If  the  size  is  * instead of being an
expression, the array type is a variable length  array  type
of  unspecified size, which can only be used in declarations
with   function   prototype   scope;122)   such  arrays  are
nonetheless complete types.   If  the  size  is  an  integer
constant  expression  and  the  element  type  has  a  known
constant size, the array type is not a variable length array
type;  otherwise,  the array type is a variable length array
type.

The final sentence there is the relevant one. Is there some reason
gcc doesn't consider 'const int' an integer constant expression when
declaring an array, but does fail to assign to a constant int after it
has been defined?

This is present in gcc-2.95.x and gcc-3.0 branch.
-- 
Gordon Sadler



More information about the Gcc mailing list