Velocity Reviews > Difference between Structure & Union

# Difference between Structure & Union

csudha
Guest
Posts: n/a

 09-10-2004
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.

Dave
Guest
Posts: n/a

 09-10-2004

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.

Kenneth Brody
Guest
Posts: n/a

 09-10-2004
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> |
+-------------------------+--------------------+-----------------------------+

Barry Schwarz
Guest
Posts: n/a

 09-10-2004
On 10 Sep 2004 07:02:08 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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>>

Vasu
Guest
Posts: n/a

 09-16-2004
Kenneth Brody <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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.