ARM Technical Support Knowledge Articles

ARE ANONYMOUS STRUCTURES SUPPORTED?

Applies to: General Topics

Answer


Information in this article applies to:


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

Article last edited on: 2004-06-10 16:34:02

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential