Question:
What is the difference between structure & union?
Ekbal Sheriff
2009-07-02 08:05:10 UTC
in c progam
Four answers:
Amit M
2009-07-02 08:18:02 UTC
=In general Terms =



Structure is a fundamental and sometimes intangible notion covering the recognition, observation, nature, and stability of patterns and relationships of entities.





Union generally means a union or combining of two individual things into one.

labour or trade union, an association of workers banded together in the interests of its members





= In "C" programming terms (computer language) =





# The term struct (short for "structure") often means a record (computer science).

In several programming languages. It also designates certain record data types, and/or is the keyword used to define such types:

* struct (C programming language)

* The struct C++ classes

* In C Sharp (programming language)

* In Cg (programming language)

* In Objective-C

* In Algol 68





# A "union" is value that may have any of several representations or formats; or a data structure that consists of a variable which may hold such a value. Some programming languages support special data types, called union types, to describe such values and variables.



## Difference in their Usage:##



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



==========
anonymous
2016-04-03 13:48:51 UTC
Structure will show allocate memory for each member while union will allocate the same memory for all members
shivam2475
2009-07-02 08:36:21 UTC
structure is extension after union

structure extension lead to classes in c++

structure is collection of different data type element..
king
2009-07-02 09:18:15 UTC
Both differ in memory allocation


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...