On Fri, 05 Sep 2003 03:30:03 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote:
> Ricardo Gibert <> wrote:
> > How can I "force" a macro to accept integer constants or expressions whose value is determined during compilation? For example, if
> > we have:
<snip>
> Yes, in C89 (which is the standard of C currently implemented by
> virtually all compilers).
>
> #define TEST(X) ((void)sizeof(int [X]), X)
>
Don't actually need the cast, the left argument of the comma operator
is already a void context.
> This requires a diagnostic from a conforming C89 compiler if X is not a
> compile-time constant. For example: <snip gcc>
> Unfortunately, most compilers are by default non-conforming, and many of
> them implement variable sized arrays as an extension, so they don't
> issue this required diagnostic unless specifically asked to run in
> conforming mode.
>
> In addition, variable sized arrays are part of the newer C99 C Standard,
> so this method also doesn't work on the few compilers that support it.
>
In the latter case could make it sizeof(struct { int foo [X]; }).
But at least gcc (2.95.2) actually supports variable-bound arrays
within a struct, and not just at the end! Bleah -- how un-C-like!
- David.Thompson1 at worldnet.att.net
|