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

m-h-l markus.lehmann@intel.com
Fri May 8 09:22:00 GMT 2015


Hi,

if I write this little code and compile with gcc-4.x.x then I get:
error: initializer element is not constant

#include <stdio.h>
#include <string.h>

typedef struct
{
    char  oneChar;
    void *ptr2;
} xxx_t;

int main( void )
{
    static xxx_t ms =  { "12345"[3], 0 };

    printf("\n\n MyOutput: 0x%x  0x%x \n\n", ms.oneChar, ms.ptr2 );
}

(I know that this code is rubbish but this just an extract from a bigger
issue, broken down to this)

What can be more constant than  "12345"[3] ?

In g++ it works ... but I have to use gcc.

Any help about how to get this solved is welcome :-)




--
View this message in context: http://gcc.1065356.n5.nabble.com/gcc-why-is-abcdef-3-not-a-constant-error-initializer-element-is-not-constant-tp1146945.html
Sent from the gcc - Help mailing list archive at Nabble.com.



More information about the Gcc-help mailing list