what is difference between structure and union in C?

+1 vote
67,895 views asked Apr 22, 2010 by anonymous

7 Answers

+3 votes
 
Best answer

Structure:

1. It allocates memory equal to sum of memory allocated to its each individual member.

2.each member have their own memory space.

3. structure can not  be implemented in shred memory.

4. it has less Ambiguity.

5. Self referential structure can be implemented in data structure.

6. All members if structure can be accessed at a time.

You can watch  Structure Video here

Union:

1.It allocates piece of memory that is Large enough to hold the Largest variable of type in union.

2. one block is used by all the members of union.

3. Union is the Best environment where memory is shared.

4. as memory is shared, Ambiguity is more in union.

5. self ref. union can not be implemented.

6. only one member is accessed at a time.

 

You can watch Union Video here

answered May 19, 2011 by anonymous
edited Nov 22, 2011 by pa
thanx  4 d info ...
really helpful....!!

can i please get your mail id ..??!!
my id is rajeev16godara@gmail.com
plz do reply  ...!! :)
really gr8 work..!!
thanks..
Thanks for this answer .really  gr8.............
thnks ....for nice ,,,,,,,,,,difference.......
t ws realy useful thanx a lot :) :) keep rocking.... :)
thankew fo the ans..'twas vyr useful:)
thanks a lot:)
tis ws really helpfull:)
very nice..and helpful of my knowledge.......thanks
i am from vit university....tamilnadu.my mob:- 769060902
0 votes
The difference between structure and union

-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
answered Apr 24, 2010 by anonymous
0 votes
The union is a structure.The main difference between structure and union isThe size of the union is equal to the size of the largest member of the union where as size of the structrue is the sum of the size of al members of the structure.And one more thing is that we can use one member of the union at a time.
answered Apr 24, 2010 by anonymous
+1 vote

Difference between structure and union in Table format

Structure Union
i. Access Members     
We can access all the members of structure at anytime. Only one member of union can be accessed at anytime.
ii. Memory Allocation     
Memory is allocated for all variables. Allocates memory for variable which variable require more memory.
iii. Initialization     
All members of structure can be initialized Only the first member of a union can be initialized.
iv. Keyword     
'struct' keyword is used to declare structure. 'union' keyword is used to declare union.
v. Syntax     
struct struct_name
{
    structure element 1;
    structure element 2;
	----------
	----------
    structure element n;
}struct_var_nm;
union union_name
{
    union element 1;
    union element 2;
	----------
	----------
    union element n;
}union_var_nm;
vi. Example     
struct item_mst
{
    int rno;
    char nm[50];
}it;
union item_mst
{
    int rno;
    char nm[50];
}it;

 

answered Nov 19, 2011 by anonymous
edited Nov 22, 2011 by pa
0 votes

In union members share memory and the memory allocated is equal to the memory allocation of data member which occupies maximum space.
In structure data members do not share memory they have their own memory and are contiguously stored.The memory allocated is equal to the sum of byte allocation of individual members.


structure:
+ point:We can access any member at any given time without data corruption.
-point:Squeezes more memory space.


Union:
+ point:Occupies less memory.
- point:We can access only one union member at a time without data loss,since the members share memory.

answered Nov 19, 2011 by anonymous
0 votes

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

3. union is best in the environment where memory is less as it shares the memory allocated.But structure can not implemented in shared memory.

4. As memory is shared,ambiguity are more in union,but less in structure.

5. self referencial union can not be implemented in any datastructure ,but self referencial structure can be implemented.

answered Nov 19, 2011 by anonymous
0 votes
Structures and Unions are used to store members of different data types.

STRUCTURE UNION

a)Declaration:

struct

{

data type member1;

data type member2;

};

a)Declaration:

union

{

data type member1;

data type member2;

};

b)Every structure member is allocated memory when

a structure variable is defined.

Example:

b)The memory equivalent to the largest item is allocated

commonly for all members.

Example:

struct emp {

char name[5];

int age;

float sal;

};

struct emp e1;

Memory allocated for structure is 1+2+4=7 bytes. 1

byte for name, 2 bytes for age and 4 bytes for sal.

union emp1 {

char name[5];

int age;

float sal;

};

union emp1 e2;

Memory allocated to a union is equal to size of the

largest member. In this case, float is the largest-sized

data type. Hence memory allocated to this union is 4

bytes.

c)All structure variables can be initialized at a time

struct st {

int a;

float b;

};

struct st s = { .a=4, .b=10.5 };

Structure is used when all members are to be

independently used in a program.

c)Only one union member can be initialized at a time

union un {

int a;

float b;

};

union un un1 = { .a=10 };

Union is used when members of it are not required to be

accessed at the same time.
answered Jan 8 by sara Guru (24,480 points)

Related questions

0 votes
1 answer 68 views
0 votes
1 answer 220 views
220 views asked Aug 16, 2011 by anonymous
+1 vote
1 answer 282 views
0 votes
1 answer 194 views
194 views asked Sep 9, 2011 by anonymous
0 votes
1 answer 98 views
...