Вы находитесь на странице: 1из 12

Structure & unions

Group of data items of


different data types held
together in a single unit.a

07/08/16

AMIT SHARMA

Declaring a Structure
struct
{
type
type
type
.
.
type
};

07/08/16

sname
var1;
var2;
var3;

varN;

struct is a keyword to define a structure.


sname is the name given to the structure
data type.
type is a built-in data type.
var1,var2,var3,..,varN are elements
of structure being defined.

Structure
template
sname is called tag makes it possible to
declare other variable of the same structure
type without having to rewrite the template
itself. Its a type name.tag is optional.

Array of Structures

If we wish to process a list of values of


structure type,then we need an array
of such structures.

07/08/16

Arrays in structures
struct student_type
{
char name[25];
int roll;
int marks;
char grade;
};
struct student stud_list[50];
07/08/16

Accessing elements an array of


structures

Individual elements of a structure in an array


of structure are accessed by referring to
structure variable name.
1.Followed by subscript.
2.Followed by dot operator.
3.Ending with structure element desired.
Suppose we want to access marks of 7 th
student,we can do so by writing

07/08/16

stud[6].marks
Rajni Bhalla

CSE/IT

WAP which reads the record of student and computes


the marks obtained by him.the structure of student
record is given below:

name

07/08/16

roll

marks

main()
{
Struct student
{
Char name[30];
int roll;
Float marks;
};
Struct student stud[30];
Int n,I;
Printf(\n enter the number of students in the class);
Scanf(%d,&n);
Printf(\nenter the records of the student);
For(i=0;i<n;i++)
{
Printf(\n name :); gets(stud[i].name);
Printf(\n roll :); scanf(%d,& stud[i].roll);
Printf(\n age :); scanf(%d,& stud[i].marks);
}
getch();
}
07/08/16

Structure within Structure


We can take object of one structure as member in another structure.
Structure within structure can be used to create complex data
applications.
Syntax:
struct time
{
int second;
int time;
int hour;
}
struct t
{
int carno;
struct time st;
struct time et;
};
struct t player;

07/08/16

Union
Union:
Union is similar as structure. The major distinction between them in
terms of storage.
In structure each member has its own storage location whereas all
the members of union uses the same location.
The union may contain many members of different data type it can
handle only one member at a time union can be declared using the
keyword union.
Hold only one object at a time
Union requires bytes that are equal to the number of bytes required
for the largest members.
Eg: union contains char,int &long then the number of bytes reserved
in the member for the union is 4 bytes.
07/08/16

Structure

Union

1. Structure allocates the memory 1. Union allocates the memory


equal to the total memory
equal to the maximum memory
required by the members.
required by the member of the
union
2.

Each member have their own


memory space

3. All the members of the


structure can be accessed at once

2. One block is used by all the


member of the union
3. Where as in an union only one
member can be used at a time.

Another important difference is in the size allocated to a


union example
structure and an union.
{ int integer;
struct example
float floating_numbers;
{ int integer;
}
float floating_numbers;
size allocated is the size of the
}
highest member.
the size allocated here is
so size is=sizeof(float);
sizeof(int)+sizeof(float);
Size of in bytes of the union is size
of size
the
The size in bytes is the sum total of
largest variable element in the
of all the elements in the structure,
union.

Union item
{
int m;
float x;
char c;
} code;
This declare a variable code of type union item.

07/08/16

THANKYOU
YOU
THANK

AMIT SHARMA
11ECU023

07/08/16

Вам также может понравиться