This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C const identifiers and constant expressions


It seems to me that I used to (this was admittedly ten plus years ago)
declare static arrays in C using const identifiers as the array length,
something like this:

static const unsigned int arraysize = 20;
static unsigned int myarray[arraysize];

but I tried doing this the other day, and couldn't get the compiler to
view the array as anything other than a variable length array.  Is
there some way short of resorting to preprocessor macros to tell the
compiler that an identifier is a compile-time constant, and is
therefore suitable for use in sizing an array?

I tried using various language standard compiler options, thinking that
it may just be that the language has changed since I remember having
that habit, but to no avail.  It could also be that I was using a
non-standard compiler back then, or even that I'm remembering a C++
habit I had that I never used with C.

I looked in the C99 standard, and sure enough, couldn't find anything
about constant expressions including identifiers declared const, so
this isn't a problem with the compiler.  But are there reasons for
this?  Are there situations where the compiler (or the coder) gets into
trouble if const identifiers are used in constant expressions?  Or is
it simply not traditional C to provide for this?

Oh - if it makes any difference, I'm on GCC v3.4.1

Thanks,

Michael Winslow






		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - Helps protect you from nasty viruses. 
http://promotions.yahoo.com/new_mail


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]