• C

stuctures abd unions

can we use structures in unions and
unions in structures
what are the uses of doing so
prasad2315Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cuziyqCommented:
I've never tried it, but I don't see why not.  Both a struct and a union are a distinct data type once you declare them, so there'd be no ambiguity to the compiler (although there might be to you).
0
Infinity08Commented:
You can use unions in structures, and structures in unions. there's no problems with that.

There's several uses to that. For example, in a struct you might want different data depending on different circumstances. A union in the struct makes that possible. For example :

        typedef struct MyData {
            int type;
            union {
                int i;
                double d;
            } data;
        } MyData;

A struct in a union can be handy for having easy access to the same data in different ways. For example :

        union {
            unsigned int i;
            struct bytes {
                unsigned char first;
                unsigned char second;
                unsigned char third;
                unsigned char fourth;
            } bs;
        } SomeUnion;
0
ozoCommented:
A struct in a union can be handy for having easy access to the same data in different ways. For example :

union {
            unsigned int i;
            struct bytes {
                unsigned char first;
                unsigned char second;
                unsigned char third;
                unsigned char fourth;
            } bs;
        } SomeUnion;

My reading of the C standard does not find a guarantee that that will work.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Infinity08Commented:
Indeed ozo, you are right. The example was quickly spat out by me without thinking too much (shame on me). Here's a better one :

        union {
            struct {
                int i;
                double d;
            } s1;
            struct {
                char c;
                float f;
            } s2;
        } someUnion;

Note that the example in my previous post IS often used, but it is not portable as ozo said !!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prasad2315Author Commented:
this topic is a bit confusing
0
Infinity08Commented:
It's pretty straightforward if you keep in mind that a struct and a union can contain any fixed length members. Since structs and unions themselves are fixed length datatypes, they can contain each other : structs can contain structs and unions (and other fixed length data types), and unions can contain structs and unions (and other fixed length datatypes).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.