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

Andrew Haley aph@redhat.com
Fri May 8 14:24:00 GMT 2015


On 05/08/2015 02:34 PM, 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."

I do.  And that language is the same evenin C11.

> Funny thing is that g++, ArmC and ICC do not have a problem with "abcdef"[3]
> as an initializer.

g++ is a different language.  C++ constants are different.  I don't
know about ArmC and ICC.  Perhaps it's an extension, or perhaps their
authors didn't properly distinguish between C and C++.

> So I wonder what shall be the sense to forbid this in gcc.

It is the standard.

> 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.

Well, you can regret writing in C if you like.  But don't argue with
us: argue with the authors of the standard.  Complaining about a
language standard to people who didn't write it is the height of
futility.  If you want C++, write in C++.  Or find some other way
around it.

Andrew.



More information about the Gcc-help mailing list