Difference between Structure & Union

Discussion in 'C Programming' started by csudha, Sep 10, 2004.

  1. csudha

    csudha Guest

    Hi All,

    Can you give me the example code which explains difference between
    Structures and Union. As a newbie please do the needful.

    Thanks,
    csudha.
    csudha, Sep 10, 2004
    #1
    1. Advertising

  2. csudha

    Dave Guest

    csudha wrote:

    > Hi All,
    >
    > Can you give me the example code which explains difference between
    > Structures and Union. As a newbie please do the needful.
    >
    > Thanks,
    > csudha.


    struct and union are defined in the same way; a structure stores its
    members at unique memory locations, and a union stores its members in
    the same place.

    struct { int a; int b; } str;
    union { int a; int b; } uni;

    str.a=1; str.b=2; printf("%d",str.a); // prints 1.
    uni.a=1; uni.b=2; printf("%d",uni.a); // prints 2.

    The usual example is of a spreadsheet that stores a number, a formula or
    some text in a cell; you have a struct for the cell that contains a
    variable indicating what is stored in the cell, and a union of a number,
    formula and string; and the value of the indicator determines to which
    member of the union you read and write.

    Dave.
    Dave, Sep 10, 2004
    #2
    1. Advertising

  3. csudha wrote:
    >
    > Hi All,
    >
    > Can you give me the example code which explains difference between
    > Structures and Union. As a newbie please do the needful.


    struct foo
    {
    char c;
    long l;
    char *p;
    };

    union bar
    {
    char c;
    long l;
    char *p;
    };

    A struct foo contains all of the elements c, l, and p. Each element is
    separate and distinct.

    A union bar contains only one of the elements c, l, and p at any given
    time. Each element is stored in the same memory location (well, they all
    start at the same memory location), and you can only refer to the element
    which was last stored. (ie: after "barptr->c = 2;" you cannot reference
    any of the other elements, such as "barptr->p" without invoking undefined
    behavior.)

    Try the following program. (Yes, I know it invokes the above-mentioned
    "undefined behavior", but most likely will give some sort of output on
    most computers.)

    ==========
    #include <stdio.h>

    struct foo
    {
    char c;
    long l;
    char *p;
    };

    union bar
    {
    char c;
    long l;
    char *p;
    };

    int main(int argc,char *argv[])
    {
    struct foo myfoo;
    union bar mybar;

    myfoo.c = 1;
    myfoo.l = 2L;
    myfoo.p = "This is myfoo";

    mybar.c = 1;
    mybar.l = 2L;
    mybar.p = "This is mybar";

    printf("myfoo: %d %ld %s\n",myfoo.c,myfoo.l,myfoo.p);
    printf("mybar: %d %ld %s\n",mybar.c,mybar.l,mybar.p);

    return 0;
    }

    ==========

    On my system, I get:

    myfoo: 1 2 This is myfoo
    mybar: 100 4197476 This is mybar

    Note how all of the "myfoo" elements are intact, whereas only the
    "mybar.p" entry is intact, as the others have been overwritten by the
    assignment to mybar.p.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Kenneth Brody, Sep 10, 2004
    #3
  4. On 10 Sep 2004 07:02:08 -0700, (csudha) wrote:

    >Hi All,
    >
    >Can you give me the example code which explains difference between
    >Structures and Union. As a newbie please do the needful.
    >

    In a union, all the members overlay each other and start at the
    beginning of the union.

    In a struct, the members do not overlap at all and are stored
    sequentially (but not necessarily adjacently) in the struct in the
    same order they are defined.


    <<Remove the del for email>>
    Barry Schwarz, Sep 10, 2004
    #4
  5. csudha

    Vasu Guest

    Kenneth Brody <> wrote in message news:<>...
    > csudha wrote:
    > >
    > > Hi All,
    > >
    > > Can you give me the example code which explains difference between
    > > Structures and Union. As a newbie please do the needful.


    Hi CSuda,

    Now you be trained, how to post Q and Doubts in this Group. Good.
    Keep it up.

    Seenivasan (Vasu)


    >
    > struct foo
    > {
    > char c;
    > long l;
    > char *p;
    > };
    >
    > union bar
    > {
    > char c;
    > long l;
    > char *p;
    > };
    >
    > A struct foo contains all of the elements c, l, and p. Each element is
    > separate and distinct.
    >
    > A union bar contains only one of the elements c, l, and p at any given
    > time. Each element is stored in the same memory location (well, they all
    > start at the same memory location), and you can only refer to the element
    > which was last stored. (ie: after "barptr->c = 2;" you cannot reference
    > any of the other elements, such as "barptr->p" without invoking undefined
    > behavior.)
    >
    > Try the following program. (Yes, I know it invokes the above-mentioned
    > "undefined behavior", but most likely will give some sort of output on
    > most computers.)
    >
    > ==========
    > #include <stdio.h>
    >
    > struct foo
    > {
    > char c;
    > long l;
    > char *p;
    > };
    >
    > union bar
    > {
    > char c;
    > long l;
    > char *p;
    > };
    >
    > int main(int argc,char *argv[])
    > {
    > struct foo myfoo;
    > union bar mybar;
    >
    > myfoo.c = 1;
    > myfoo.l = 2L;
    > myfoo.p = "This is myfoo";
    >
    > mybar.c = 1;
    > mybar.l = 2L;
    > mybar.p = "This is mybar";
    >
    > printf("myfoo: %d %ld %s\n",myfoo.c,myfoo.l,myfoo.p);
    > printf("mybar: %d %ld %s\n",mybar.c,mybar.l,mybar.p);
    >
    > return 0;
    > }
    >
    > ==========
    >
    > On my system, I get:
    >
    > myfoo: 1 2 This is myfoo
    > mybar: 100 4197476 This is mybar
    >
    > Note how all of the "myfoo" elements are intact, whereas only the
    > "mybar.p" entry is intact, as the others have been overwritten by the
    > assignment to mybar.p.
    Vasu, Sep 16, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    25,242
    Lal Bahadur Singh
    Nov 11, 2011
  2. manish sahu
    Replies:
    5
    Views:
    5,168
    James Kanze
    Feb 14, 2008
  3. Nick Timkovich
    Replies:
    0
    Views:
    61
    Nick Timkovich
    Feb 25, 2014
  4. Skip Montanaro
    Replies:
    0
    Views:
    58
    Skip Montanaro
    Feb 25, 2014
  5. Peter Otten
    Replies:
    1
    Views:
    62
    Mark Lawrence
    Feb 25, 2014
Loading...

Share This Page