Keil Logo

GENERAL: ARE ANONYMOUS STRUCTURES SUPPORTED?


Information in this article applies to:

  • C166 Version 3.12
  • C166 Version 4.03
  • C251 Version 2.14
  • C51 Version 5.50

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 the following URL for more information.

http://msdn.microsoft.com/library/devprods/vs6/visualc/vccore/_langref_anonymous_structures.htm

Last Reviewed: Thursday, June 10, 2004


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.