Ask Experts » Computers » Software »

C question : Difference between structure and union


Date: 15 Jul 2009   Posted By: raguraman     Group: Computers    Category: Software   

What is difference between structure and Union?



Author: Sunil Saharan    15 Jul 2009      Member Level: Gold     Points : 6  (Rs 5)    Voting Score: -1

Dear Friend,
The concept of structure and union are same. Both are created to collect different data type in one variable. The only difference is in memory management i.e. Memory requirement of the two. If we declared a structure with 1 char and two int variable then it we use 5byte of RAM while we execute program. If we declare struct1, struct2 of the above structure type then both structures will acquire 5 byte each i.e. Total 10 bytes will be used of RAM. On the other hand if we declare union with 1char and 2 int variable and declare to instance uni1 and uni2 then they will acquire only 5 bytes. If we access uni1 first then data we entered will be stored at uni1's variable and if we again enter value of variables for uni2 then these will be overwriten of the uni1 variables.
Advantage of union over structure : -
Less RAM space is required thus fast execution of program.
DisAdvantage of Union over structure : -
If we use 2 or more instance of single onion the data will be lost after data for second instance is entered.
I think the concept of union and structure is clear to you.
Best Regards,
Sunil Saharan
Working for an ideal and peaceful society.
____________
Thought of the day : -
No candle lost its light while lightning another.


Author: UltimateRengan    16 Jul 2009      Member Level: Gold     Points : 5  (Rs 3)    Voting Score: 0

The main difference between structure and union is

The size of the structure is sum of the size of each member in the struchture.but size of the union is size
of Largest member in the union because union members are overlaps on each other in memory.
If we declare two structure variables,Both variables are stored in different location
but union stored in same location.


General format of structure : -

struct tag_name
{
data type member1;
data type member2;


}

Example of Structure:-

struct lib_booker
{
char titles[20];
char authors[15];
int page;
float prices;
};

General format of union : -

union item
{
int m;
float p;
char c;
}
code;


Author: satyendra singh    17 Jul 2009      Member Level: Silver     Points : 2    Voting Score: 0

Dear Friend,

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.

The difference between structure and union are:

1. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.
2. In union one block is used by all the member of the union but in case of structure each member have their own memory space



Author: Syam.S    17 Jul 2009      Member Level: Silver     Points : 2    Voting Score: 0

While structure enables us treat a number of different variables stored at different in memory , a union enables us to treat the same space in memory as a number of different variables. That is a Union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion.
There is frequent rwquirement while interacting with hardware to access access a byte or group of bytes simultaneously and sometimes each byte individually. Usually union is the answer.
Difference With example***** Lets say a structure containing an int,char and float is created and a union containing int char float are declared. struct TT{ int a; float b; char c; } Union UU{ int a; float b; char c; }
sizeof TT(struct) would be >9 bytes (compiler dependent-if int,float, char are taken as 4,4,1)
sizeof UU(Union) would be 4 bytes as supposed from above.If a variable in double exists in union then the size of union and struct would be 8 bytes and cumulative size of all variables in struct.
Detailed Example:
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

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



Author: harish    17 Jul 2009      Member Level: Bronze     Points : 1    Voting Score: 0

A simple answer is Structure occupies memory for its variables
where as an Union will not take memory for all its variables
It will take the memory occupied by the highest memory
occupying variable.


Author: Raju    18 Jul 2009      Member Level: Silver     Points : 2  (Rs 1)    Voting Score: 0

Dear Friend
I have a document to clarify your doubt.. please
check my attachment


STRUCTURES & Unions.doc

Author: swapna    29 Sep 2009      Member Level: Gold     Points : 2    Voting Score: 0

STRUCTURES :
A structure is a procedure capable of storing data belonging to different data types.

UNION :
A union refers to an element holding objects of different properties (say types,sizes,etc.,)

DIFFERENCES BETWEEN STRUCTURES AND UNIONS ::

There are six differences as follow :-

1.Declaration of structure is
struct
{
datatype member1;
datatype member2;
}
Declaration of union is
union
{
datatype member1;
datatype member2;
}

2.Every structure member is allocated memory when a structure variable is defined.whereas in union the memory equivalent to largest item that is allocated comonly for all members.

for example :

struct
{
char c;
int x;
float y;
}s;

memory allocated for s is ,1+2+4=7bytes

Example for union :

uniom tag
{
char c;
int x;
float f;
}u;

the memory allocated for union is 4bytes(largest size among members)

3. In union only one member can assign values at a time.But in structure all the members can assign at a time.

4.only one union member can be initialised at a time
for example
union un
{
int x;
float y;
}
union un x=12;

But in structures
struct st
{
int x;
float y;
}
struct st s={10,12}

4. memory can be allocated dynamically in structures,where as in union can not be alloacted dynamically.

5.structure is a user-defined data type.
union is a derived datatype.

6. In structures value assigned to one member cannot cause the change in other members ,where as in union it cause change in values of other members.


Author: Shobha Manasa    29 Sep 2009      Member Level: Gold     Points : 4  (Rs 2)    Voting Score: 0

Hi

The difference between structure and union.
Answer:

There only a difference in the storage of memory or both these structure and union.
A structure creates a format that may be used to declare structure variables.
Consider an example: A book database consisting of book name, author, number of pages and price.
The structure is defined as follows

struct book_db
{
char title[20];
char author[15];
int pages;
float price;
}book1;

Here book1 is a structure variable that can store book name, author, price and number of pages in it. Each element of this structure variable has a different memory allocated.
title author etc are members or element of the structure book_db.

Unions are concept borrowed from structures and therefore follow the same syntax as structures. The major distinctions is in the storage. In structures, each member has its own storage location, where as all members in union has same memory location. This implies that thought a union may contain n number of fields but it can handle only one member at a time. The memory stored for unions is calculated by the highest memory location occupied by the largest member.

For suppose, in the following union

union abcd
{
int a;
float b;
char c;
}x;

x is a union variable here. the memory allocated for the union variable x is that allocated for a float variable because the float occupies the larger memory when compares to int and char.


Author: chhaya kaushik    29 Sep 2009      Member Level: Bronze     Points : 1    Voting Score: 0

Structure and union both are almost same.The only difference is in memory management i.e. Memory required by these two. Union require less RAM than structures.

Author: prakash bhatnager    04 Oct 2009      Member Level: Silver     Points : 2    Voting Score: 0

Hello Raguraman

The difference between structure and union in c are:
Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

In union one block is used by all the member of the union but in case of structure each member have their own memory space


Post Answer



You must Sign In to post a response.


Post Answer        





Related Questions
  • Return to Question Index
  • Ask Question to Experts

  • Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.