Keil Logo

GENERAL: Are Anonymous Structures Supported?


Information in this article applies to:

  • C166 Version 4.03 and later
  • C251 Version 2.14 and later
  • C51 Version 5.50 and later

SYMPTOMS

Does the Keil Compiler support anonymous structure definitions inside a union? For example:

union Message {
  struct {
    char aa;
    char bb;
    char cc;
    };
  long dd;
  };

union Message msg;

msg.aa = 0xAA;
msg.bb = 0xBB;
msg.cc = 0xCC;

if (0x00AABBCC == msg.dd)...

CAUSE

The problem here is that the structure in your example is not given a structure member name. Therefore, in standard C, there is no way to access it.

RESOLUTION

If you change the example to the following it will work.

union Message {
  struct {
    char aa;
    char bb;
    char cc;
    } s;
  long dd;
  };

union Message msg;

msg.s.aa = 0xAA;
msg.s.bb = 0xBB;
msg.s.cc = 0xCC;

STATUS

Note that anonymous structures are a Microsoft variant of C. This is not an ANSI C compatible construct.

MORE INFORMATION

The following information was copied from the Microsoft support web site.

Anonymous Structures Microsoft Specific :

A Microsoft C extension allows you to declare a structure variable within another structure without giving it a name. These nested structures are called anonymous structures. C++ does not allow anonymous structures.

You can access the members of an anonymous structure as if they were members in the containing structure.

Refer to Structure Declarations for more information.


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.