gcc: why is "abcdef"[3] not a constant (error: initializer element is not constant)

msebor@redhat.com msebor@redhat.com
Fri May 8 15:19:00 GMT 2015


On 05/08/2015 08:34 AM, m-h-l wrote:
> I guess you refer to this part of the standard:
>
> "The array-subscript [] and member-access . and -> operators, the address &
> and indirection * unary operators, and pointer casts may be used in the
> creation of an address constant, but the value of an object shall not be
> accessed by use of these operators."
>
> Funny thing is that g++, ArmC and ICC do not have a problem with "abcdef"[3]
> as an initializer.
> So I wonder what shall be the sense to forbid this in gcc.
> This way I cannot use gcc to calculate e.g. CRCs or Hashs over real constant
> things at build time without extra tools, even not in such a clear case as
> here. With the other compilers its no problem.

It is true that the C standard requires a constant expression in this
context and that the bracket expression is not such an expression, so
the program isn't strictly conforming.

However, the C standard leaves room for implementations to provide
extensions as long as they don't change the behavior of strictly
conforming programs. Unless such a program can be constructed that
would detect such an extension, providing it wouldn't make
an implementation wrong (non-conforming). GCC has a rich set of
extensions so there is a precedent for such a request if one should
be made. I would encourage you to open a Bugzilla and include your
arguments in it.

Martin



More information about the Gcc-help mailing list