[comp.lang.c] const in array bound expression

preston@peritus.UUCP (Preston Gardner) (08/02/88)

What constitutes a constant expression under the new ANSI standard?  The
following should be legal:

    const int i = 5;
    int a[i];

But what about this?  This offers a big opportunity for headaches.

    const int agg[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34 };
    int a[agg[4]];

I could not find anything in the ANSI draft I have (May 13, 1988) to help
with this.

C++ allows the first case but the AT&T C++ preprocessor we have rejects
the second.


Does anybody out there have any experience with this?  I believe the correct
thing is to allow scalar const vars (whose value is known in this module --
not externs) to be used in array bound expressions.  But I don't see it
in the ANSI standard, and I don't have any ANSI standard compilers (except the
one I'm writing) to try it out on right now.
-- 
				-- Preston Gardner
				   Peritus International, Inc.
				   ..{pyramid,sun}!oliveb!peritus!preston

karl@haddock.ISC.COM (Karl Heuer) (08/04/88)

In article <123@peritus.UUCP> preston@peritus.UUCP (Preston Gardner) writes:
>What constitutes a constant expression under the new ANSI standard?  The
>following should be legal:
>    const int i = 5;
>    int a[i];

It isn't, though.  I believe this is what Bjarne meant when he said that when
X3J11 took |const| from C++, they "watered it down".

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

henry@utzoo.uucp (Henry Spencer) (08/06/88)

In article <123@peritus.UUCP> preston@peritus.UUCP (Preston Gardner) writes:
>    const int agg[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34 };
>    int a[agg[4]];

Check the restrictions on the operands permissible in constant expressions;
I believe you will find that "agg" is not legal in an integer constant
expression, which is what's wanted in that context.

(At least, this is the way it is in the January draft; I have no idea what
the May draft says, since I haven't seen it yet.  Grr.)
-- 
MSDOS is not dead, it just     |     Henry Spencer at U of Toronto Zoology
smells that way.               | uunet!attcan!utzoo!henry henry@zoo.toronto.edu