union - Bold/Underlined is a question mark

1 view
Skip to first unread message

Viral Mehta

unread,
Jul 14, 2016, 6:15:11 AM7/14/16
to fundamental...@googlegroups.com
#include <stdio.h>
struct inside {
        int a;
        char b;
        int c;
};

struct bigger {
        struct inside in;
        int d;
};
union outside {
        struct inside in;
        struct bigger big;
};

int main()
{
        union outside out;

        out.big.in.a = 9; //ASSIGN TO BIG
        out.big.in.b = 'a';
        out.big.in.c = 10;
        out.big.d = 11;

        //GET IT FROM SMALL
        // WILL THIS ALWAYS WORK ???
        printf(" %d : %c : %d : %d\n", out.in.a, out.in.b, out.in.c, out.big.d);


}


--
Thanks,
Viral Mehta

Vishal Thanki

unread,
Jul 14, 2016, 6:30:05 AM7/14/16
to fundamental...@googlegroups.com
On Thu, Jul 14, 2016 at 12:15 PM, Viral Mehta <vira...@gmail.com> wrote:
> #include <stdio.h>
> struct inside {
> int a;
> char b;
> int c;
> };
>
> struct bigger {
> struct inside in;
> int d;
> };

If the members declared in "bigger" structure is shuffled (order of
declaration is changed),
it may not work.

Is that what you were looking for?

> union outside {
> struct inside in;
> struct bigger big;
> };
>
> int main()
> {
> union outside out;
>
> out.big.in.a = 9; //ASSIGN TO BIG
> out.big.in.b = 'a';
> out.big.in.c = 10;
> out.big.d = 11;
>
> //GET IT FROM SMALL
> // WILL THIS ALWAYS WORK ???
> printf(" %d : %c : %d : %d\n", out.in.a, out.in.b, out.in.c,
> out.big.d);
>
>
> }
>
>
> --
> Thanks,
> Viral Mehta
>
> --
> -- Owner of this group do not accept ANY liability for the views expressed
> in the email or for the consequences of any computer viruses that may be
> transmitted with this email. Owner is not responsible for any kind of
> copyright violations or infringements caused/posted by any user of the
> group. Messages are posted through moderation also does not include any kind
> of verification against copyright violations or infringements.
> ---
> You received this message because you are subscribed to the Google Groups
> "insane" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to fundamental-discu...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Viral Mehta

unread,
Jul 14, 2016, 6:46:39 AM7/14/16
to fundamental...@googlegroups.com
Ah, shuffling is the condition that i did not put.... It has to be first member always...

I have seen this normally where you have same header for different operations and actual result differs for some.... In that case, you use this pattern
--
Thanks,
Viral Mehta
Reply all
Reply to author
Forward
0 new messages