public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Var length array's vs. const int
@ 2001-08-11 10:54 Gordon Sadler
  2001-08-11 11:08 ` Joseph S. Myers
  0 siblings, 1 reply; 3+ messages in thread
From: Gordon Sadler @ 2001-08-11 10:54 UTC (permalink / raw)
  To: gcc

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Var length array's vs. const int
  2001-08-11 10:54 Var length array's vs. const int Gordon Sadler
@ 2001-08-11 11:08 ` Joseph S. Myers
  2001-08-11 12:09   ` Gordon Sadler
  0 siblings, 1 reply; 3+ messages in thread
From: Joseph S. Myers @ 2001-08-11 11:08 UTC (permalink / raw)
  To: Gordon Sadler; +Cc: gcc

On Sat, 11 Aug 2001, Gordon Sadler wrote:

> The final sentence there is the relevant one. Is there some reason
> gcc doesn't consider 'const int' an integer constant expression when

Re-read 6.6, which defines integer constant expressions.  Compare ISO/IEC
14882 subclause 5.19 [expr.const] which defines them for C++, and be
careful not to confuse C and C++ here.

-- 
Joseph S. Myers
jsm28@cam.ac.uk

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Var length array's vs. const int
  2001-08-11 11:08 ` Joseph S. Myers
@ 2001-08-11 12:09   ` Gordon Sadler
  0 siblings, 0 replies; 3+ messages in thread
From: Gordon Sadler @ 2001-08-11 12:09 UTC (permalink / raw)
  To: gcc

On Sat, Aug 11, 2001 at 07:08:18PM +0100, Joseph S. Myers wrote:
> On Sat, 11 Aug 2001, Gordon Sadler wrote:
> 
> > The final sentence there is the relevant one. Is there some reason
> > gcc doesn't consider 'const int' an integer constant expression when
> 
> Re-read 6.6, which defines integer constant expressions.  Compare ISO/IEC
> 14882 subclause 5.19 [expr.const] which defines them for C++, and be
> careful not to confuse C and C++ here.
> 
Thanks for pointing this out. I've been mixing the languages at various
times, and never had the actual standard until recently.

If I read it correctly, the difference lies in the c++ standard allowing
for const variables to qualify as an expr.const. Thanks for the
clarification.

-- 
Gordon Sadler

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-08-11 12:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-11 10:54 Var length array's vs. const int Gordon Sadler
2001-08-11 11:08 ` Joseph S. Myers
2001-08-11 12:09   ` Gordon Sadler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).