initialization of static struct with GCC 3.0.1

Thanh-Lam NGUYEN nguyen56@sxb.bsf.alcatel.fr
Tue Sep 18 00:34:00 GMT 2001


Hi,
here is the code I tried to compile :
---------------------------------------------------
#include <stdlib.h>
#include <stdio.h>

typedef union {
    long l;
    struct {
        short ll;
        short lh;
    };
} tunion;

const tunion initunion={ 0x11223344l };

int main( void )
{
    static tunion t=initunion;

    printf( "t.l  : %#lx\nt.ll : %#lx\nt.lh : %#lx\n", t.l, t.ll, t.lh
);
}
---------------------------------------------------
nothing very special.
Here's the message I get :

>gcc union.c
union.c: In function `main':
union.c:16: initializer element is not constant
Exit 1


When I remove the  "static" prefix, it compiles fine.

What I want to do is to initialize my union once at the begining of a
function and from then,
I want it to retain its value each time I call the function.

Is there another way to do it ?
Currently I use a if statement to do it, but I need to optimize my code
to gain speed.

Thanks for any reply
Thanh lam NGUYEN



More information about the Gcc-help mailing list