[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

stuctures abd unions

can we use structures in unions and
unions in structures
what are the uses of doing so
0
prasad2315
Asked:
prasad2315
3 Solutions
 
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
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now