Jim Meyering wrote:
> Why not use an enum, as you suggested above?
>
> enum { CUTOFF = 1024 };
> char buf[CUTOFF];
That works only for 'int' constants, not for 'unsigned int' or 'long'.
Also, HP-UX and AIX compilers are known to have bugs in this area. (See
lib/malloca.h.)
Also, the use of 'enum' to define constants is likely to surprise people,
since - while technically correct according to ANSI C - it is not the
mainstream style.
> Pádraig and I were discussing that in the context of coreutils'
> new truncate.c. I'd like to be able to use the attribute
> (though not for cases like the above, where enum is fine)
> to mark variables as write-once.
There is no portability problem with that, as far as I know. gcc versions
around gcc-2.6 had problems with 'const' arrays, but that's more than 10
years ago.
> However, I will do that
> only if I can automate the check for compliance that they've
> all been removed after applying a c99-to-c89 patch
There's nothing to remove, IMO. 'const' variables are valid in C89 as well.
The only problem is the use of such variables as array sizes - this makes
use of the "variable length arrays" feature of C99.
> Do any of you know of a gcc option to warn about
> "const-applied-to-scalar-type"?
There is nothing to warn about (except possibly in function prototypes).
Bruno