Your Only Destination To Empower Your Computer Programing Knowledge. Sky is not so high, You can Touch it if You Try

structure and union

Hello friends,
How are you? Today is rainy day in sirsa.Mood is fresh so i decide to write about structure and union.friends let us discuss about structure and union.
STRUCTURE:-Structure is a heterogenous combinations of data types.It is user defined data type.
Syntex:-
Struct struct_name
{
struct element 1;
struct element 2;
.
.
.
struct element n;
}struct_object;
Ex.
Struct employee
{
int id;
char name[5];
int age;
int salary;
}e1,e2;

UNION:-Union is also a user defined data type as structure but there is only some difference for accessing elements,memory allocation and syntex,which i discuss later.

Syntex:-
Union employee
{
int id;
char name[5];
int age;
int salary;
}e1,e2;

Difference between structure and union:-
1.Structure elements can be accessed more then one at one time but only one element of union can be accessed at once.

2.Structure elements store at separate location each but elements of unions share same memory space.
3.Memory space for each elements in structure allocate as per data type but in union only space required maximum element type is allocate and share by other elements too.
4.Elements of structure can be initialized at the time of declaration but not posible in case of elements of union.

No comments:

Post a Comment