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.

anonymous structs?



I'm porting some code from IAR to CCE and getting a "declaration does not declare anything" warning..  Does this mean that anonymous structs are not supported? 

code:

union rgb_t
{
struct { float r, g, b; };
float asArray[3];
};

Are there any flags or workarounds I'm missing?

Thanks!

Tyler
  • Did you use "rgb_t" anywhere else in this module?

    If not, then the waring is valaid. You can either delete that union declaration or keep it and ignore the warning.

    If "rgb_t" is used elsewhere, then CCE is incorrect. You can still ignore the warning.

  • A struct declaration can have a type "tag" or can be anonymous. But it must have an actual declaration name in order to create something.

       struct <tag> { float r, g, b; } <declaration>;

    Without the <declaration>, this line contributes nothing to the union and the r, g, and b elements will not be accessible from the union.

    If you were to declare a union as

    union rgb_t myUnion;

    then you would access the elements using

    myUnion.asArray[0] = 1.1;
    myUnion.asArray[1] = 2.2;
    myUnion.asArray[2] = 3.3;

    but there is no way to access the r, g, and b.without the declaration. If instead you use

    union rgb_t
    {
       struct { float r, g, b; } asStruct;
       float asArray[3];
    };

    then you can get to those elements as

    myUnion.asStruct.r = 4.4;
    myUnion.asStruct.g = 5.5;
    myUnion.asStruct.b = 6.6;

  • Hi, thanks for the replies.

    I think I wasn't very clear in describing my problem. In IAR (and gcc, msvc) I can do this:

    union rgb_t
    {
    struct { float r, g, b; };
    float asArray[3];
    };

    rgb_t color = {1,1,1};
    color.r = .5;
    float green = color.g;
    float blue = color.asArray[2];

    I often see this [non-standard] feature referred to as "anonymous structs/unions" which is of course misleading. Google tells me that the gcc docs refer to it as "Unnamed struct/union fields within structs/unions."

    For some reason I thought CCE used some flavor of gcc, but I guess not. Not a terribly big deal, but it's a very handy extension to the language.. sigh.

  • Anonymous Struct/union is not allow in C Language .

     

    But IAR has used their own Language Extensions by default which is allow to use anonymous struct/union. 

     

    And in gcc, you can use anonymous struct/union too by add  "-fms-extensions" in Makefiles.

     

    For more details you can read from  http://gcc.gnu.org/onlinedocs/gcc-3.4.5/gcc.pdf ,

    Chapter 5: Extensions to the C Language Family

    5.53  Unnamed struct/union fields within structs/unions.

     

    Good Luck!!

  • Hi, thanks for the reply, but the question was about CCE, not gcc.  (And I mentioned in my second post that, as you say, this is possible in IAR and gcc.)

**Attention** This is a public forum