Friday, May 16, 2008

What use of structure and union?

structure is a user defined data type,that is collection of hetrogeneous data types. if we have to assign memory to a record.for eg,book record,we can declare it as
struct book
{
char name[20];
int pages;
fload price;
};
struct book s1,s2;

in last line we have declared two variable of struct type,now the basic data types variables will be recognized in the form of structure. and the memory allocated to above structure will be char(1*20)+int(2)+ float(4)= 26 bytes.
Union is same as structure,except the difference that the memory allocated will be the highest data type memory,as in this case 20 bytes.because char type data name is taking highest memory,and this memory will be shared among all variables declared in the union.

No comments:

Contributors