Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


unions & structs

Posted on 1999-07-08
Medium Priority
Last Modified: 2010-05-18
Can we have anonymous unions and structs in the C programming language.
Question by:tausifaf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1263388
union - yes. struct - no.

Accepted Solution

apmontgo earned 100 total points
ID: 1263389
Actually, you can have both, but they are messy and rarely used. Examples:

struct {int x, y} myPoint;
union {char a; int i} intOrChar;

myPoint.x = 4;
myPoint.y = 6; // coordinate of (4,6)

intOrChar.i = 7; // intOrChar is treated as an int == 7
intOrChar.a = 'a'; // intOrChar is treated as a char == 'a'
//note intOrChar.i is garbage once you use intOrChar.a

the names of the union and struct are anonymous. Hope this helps.
LVL 11

Expert Comment

ID: 1263390
Yonat is right.  However:

"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."

Other compilers may support this non-standard feature as well.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 11

Expert Comment

ID: 1263391
apmontgo is wrong :-(

Expert Comment

ID: 1263392
apmontgo, an anonymous struct would be a struct without *any* name, even that of a struct variable. Here is an example of an anonymous union:

struct external {
    union {
        int n;
        float x;
} var;

var.n = 7;
var.x = 3.14;

Get it?
LVL 11

Expert Comment

ID: 1263393
Funny, an incorrect answer was accepted.

Author Comment

ID: 1263394
I had not accepted the answer that you guys had proposed. I got the answer from a friend of mine. Anyway, I did not wanted to pursue the discussion further. Thanks for the feedback.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question