Difference between Structure & Union

C

csudha

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

Dave

csudha said:
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.
 
K

Kenneth Brody

csudha said:
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.
 
B

Barry Schwarz

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

Vasu

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top