Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Difference between Structure & Union

Reply
Thread Tools

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.
 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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> |
+-------------------------+--------------------+-----------------------------+


 
Reply With Quote
 
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>>
 
Reply With Quote
 
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between c structure and c++ structure raghunandan_1081@yahoo.com C++ 9 11-11-2011 07:34 AM
difference between structure and union ,, can anyone help me? manish sahu C++ 5 02-14-2008 11:23 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM



Advertisments