3. STRUCTURE
Structure is user define data type. When we defined a
structure as a group of elements of different data-type
held together in a single unit .The individual
elements are called data members of Structure.
Members of the structure can be accessed &
processed separately. The graphical representation
of a structure is :
Structure name
Member-1
Member-2
Member-3
Member-n
5. ACCESSING MEMBERS OF STRUCTURE
The members of a structure variable are accessed using dot
operator(.). The syntax is
structure-variable. member name
Example: struct date
{
int dd;
int mm;
int yy;
}
dob;
The values to members of structure variable dob are assigned
as:
dob.dd=05;
dob.mm=07;
dob.yy=2014;
6. INITIALIZATION OF STRUCTURES
Like variables of other data types, a structure
variable can be initialized. For illustration consider
the following segment.
struct std
{
int rollno;
char name[25];
char Dept[25];
};
struct std student={44,”Divesh”,”IT”};
7. FUNCTIONS & STRUCTURES
In C ,structure can be passed to a function as
a single variable. The scope of a structure
declaration should be an external storage
class whenever a function in the main
program is using a structure data type. The
member data should be same throughout the
program. Individual structure members can
be passed to a function as arguments in the
function call & a single structure member can
be returned through the return statement.
8. POINTER & STRUCTURE
Through the use of (&) address operator, starting
address of a structure can be accessed in the
same manner as any other address. Therefore, if
variable represents a structure type variable the
& variable represents the beginning address of
that variable. A pointer variable for a structure
declare as :
type *ptr
to assign the starting address of a structure
variable to pointer as
ptr=&variable
9. Example:
struct
{
int acc_no;
char acc_type;
char customer[50];
float bal_amount;
}
s_customer,*ptr_cust;
Pointer structure can be accessed & processed in
following way:
(*structure_name).field name_variable;
10. ARRAY OF STRUCTURES
When we want to process a list of
values of structure type, it is
required to use an array of
structures. The array of structures
are defined as simple structures,
only the difference is that instead
of a simple variable, we specify
array variable name for structure.
11. Example:
struct stud
{
int rollno;
char name[25];
char dept[25];
int height;
};
struct stud student[100];
12. NESTED STRUCTURES
A structure can be embedded in another structure. The concept
of nested structure illustrated by following example:
struct name
{
char fname[20];
char mname[20];
};
struct sal
{
char empno[5];
char dept[15];
float salary;
}
employee;
13. UNION
Union is quite similar to structure but processing & storage
is little different. Both contains a group of members
which may be off different data types but in structure
each member has its own memory location, whereas in
union all the members share the same storage area.
Syntax- Union tag-name
{
data-type member1;
data-type member 2;
-----------------------------
data-type member n;
};
14. DIFFERENCE
Structure Union
Structure allocates storage space for
all its members separately.
Union allocates one common storage
space for all its members.
Union finds that which of its member
needs high storage space over other
members and allocates that much space
Structure occupies higher memory
space.
Union occupies lower memory space
over structure.
We can access all members of structure
at a time.
We can access only one member of
union at a time.
All members may be initialized. Only first member may be initialized.
Consume more space than union. Conservation of memory is possible.
Syntax: struct name
{
data-type member-1;
data-type member-2;
----------------------
data-type member-n;
}
Syntax: Union tag-name
{
data-type member1;
data-type member 2;
-------------------------
data-type member n;
}