This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler issue for one union with multi-structure

Dear all

I meet a compiler issue when I use the following code

How could I modify its to solve this error? (version: CCS 5.3, chip:LM4F232)

Thanks :-)

typedef union{
    unsigned long pack;
    struct {
        unsigned char db0; //data byte 0.
        unsigned char db1; //data byte 1.
        unsigned char db2; //data byte 2.
        unsigned char db3; //data byte 3.
    };


    struct {
        unsigned short dwl; //low word of data.
        unsigned short dwh; //high word of data.
    };

}PROTOCOL_DATA;

typedef struct register {
    unsigned long     destination;
    PROTOCOL_DATA protocol;

}REG;

  • What is the compiler Error? Can you post the logs..?

  • I wanna assign the value to the member

    REG reg;

    reg.protocol.dwl = 1;

    But the compiler shows the following error message...

    #137 union "<unnamed>" has no field "dwl" 

  • You are trying to use a GCC extension to the C language called unnamed fields.  The TI compiler supports most (but not all) GCC language extensions via the build switch --gcc.  Please see this wiki page for more background on support for GCC extensions.  When I add --gcc, I still get this error ...

    C:\dir>armcl --gcc --verbose_diagnostics file.c
    "file.c", line 18: error: expected either a definition or a tag name
    typedef struct register {
                   ^

    1 error detected in the compilation of "file.c".

    >> Compilation failure

    If I remove "register" from that line

    typedef struct /* register */ {

    then I get a clean build.

    Thanks and regards,

    -George

  • Hi kuolun tu,

    Error is expected when you try to access the structure because you forgot to give name to your structure !! 

    Try giving some valid names to both the structures declared inside the union.

    Please Refer below:

    typedef union{
        unsigned long pack;
        struct {
            unsigned char db0; //data byte 0.
            unsigned char db1; //data byte 1.
            unsigned char db2; //data byte 2.
            unsigned char db3; //data byte 3.
        }STRUCTURE_1;


        struct {
            unsigned short dwl; //low word of data.
            unsigned short dwh; //high word of data.
        }STRUCTURE_2;

    }PROTOCOL_DATA;

    typedef struct register
    {
        unsigned long     destination;
        PROTOCOL_DATA protocol;

    }REG;

    Now, declare & access:

    REG reg;
    reg.protocol.STRUCTURE_2.dwl = 1;

    Hope this solves your problem