Unit V
Unit V
UNIT V
STRUCTURES
SAMPLE QUESTIONS
Definition:- The collection of similar (or ) different data-type items or values is called
Structure.
Accessing structures:- Accessing structure means accessing the structure members. That
means storing the values into structure members and displaying the values from structure
members.
The dot(.) operator is used for accessing the structure members. It forms the link between
structure variable and it’s member.
Syntax:- structure variable . member ;
Example:-
x.rollno=1;
strcpy(x.name,"hari");
x.per=78.23;
3
Structure Intialization: -
Values are assigned at the time of structure defitnion is called Structure Initialization.
Syntax:- struct structureName var1={list of values},var2={list of values},....,
var n={list of values};
Example :- struct StudentDetails x={1,”hari”,75.15},y={2,”siva”,78.25};
Output:-
Roll Number=1 Name=hari Percentage=78.23
4
Pointers to structures
The variable which is the pointer for a structure is called pointer-to-structure. That means
here we are using the pointer as structure variable.
Syntax:-
struct StructureName *ptr1,*ptr2,......*ptr n;
Structure pointer Variable->Member;
We can access the structure members by the pointers using arrow operators arrow(->)
Example:-
struct StudentDetails
{
int rollno;
char name[20];
float per;
};
Nested Structure: -Nested Structure means the structure which contains the another
structure variable as it’s member.
The diagrammatical representation of nested structure is ,
struct date
{
int day;
int mon;
int year;
};
struct StudentDetails
{
int rollno;
char name[20];
float per;
struct date dob;
};
struct StudentDetails x ;
In the above example , StudentDetails structure contains the date structure variable
as it’s member. It is called Nested Structures.
Members of the nested structure is accessed by using the following format.
In the above example StudentDetails is the External structure and date is the internal
structure. Now we can access the internal structure members as follows:
x.dob.day=3;
x.dob.mon=8;
x.dob.year=1998;
6
Output:-
Student Details:
Roll Number=1 Name=hari Percentage=78.23
7
arrays of structures:-
Array of structure means assigning size to the structure variable.
Consider the following example:
Example: sttuct StudentDetails x[5];
Syntax : struct StructureName var[size];
Consider the following explanation for what is the need to implement Array of
structures.
For displaying the 5 student details we need 5 structure variables as,
sttuct StudentDetails x,y,z,w,t;
But by using the array of structure we can display the 5 student details using only one
structure variable as,
sttuct StudentDetails x[5];
Output:-
Enter 3 student details:
1 hari 78.23
2 siva 58.32
3 sree 65.32
3 student details:
Roll no=1 Name=hari Percentage=78.23
Roll no=2 Name=siva Percentage=58.32
Roll no=3 Name=sree Percentage=65.32
8
Typedef:-
Type definition allows us to renaming the existing built-in data-types and user-defined data-
types.The main purpose of the typedef is increasing the readability of the programming code.
display(x);/*calling function*/
display(&x);/*calling function*/
void main()_
{
struct StudentDetails x;
clrscr();
x.rollno=10;
strcpy(x.name,”sai”);
x.per=96.73;
display(x);
getch();
}
Output:-
10 sai 96.73
10
Bit-fields
The collection of adjacent bits in a word of memory is called Bit-fields. The main purpose of
the bit-field is to utilize the memory in effective manner that means wastage of memory is
eliminated by specifying the size.
{ struct StudentDetails
data-type member1:size; {
data-type member2:size; int rollno:7;
data-type member3:size; char name[20];:32
--------------------------------- float per:6;
---------------------------------- };
data-type member n:size;
};
Roll no 2 1
Name 20 4
Per 4 1
Total 26 6
By the above table we can say that for performing a task using bit fields, it needs 6B
instead of 26B.So,20B of memory wastage is reduced.
The diagrammatical representation of memory using Bit-field is shown below
void main()
{ struct StudentDetails x;
int length;
clrscr();
length=size of (x);
x.rollno=1;
strcpy(x.name,"hari");
x.per=78.23;
printf("Student Details:\n");
printf("Roll Number=%d Name=%s Percentage=%f\n",x.rollno,x.name,x.per);
printf(“no of bytes=%d”,length);
getch();
}
Output:-
No.of Bytes=6.
Union
Union is similar to structure that means it is also defined as, the Collection of the different
(or) similar data items The major different between union and structure is, in case of
structure the number of memory locations allocated is equal to the no. of structure members.
But in case of union only one memory location is allocated whose capacity is equal to the
largest data-type union member, but this memory is accessed by any union member one at a
time.
Union declaration:-
{ union StudentDetails
data-type member1; {
data-type member2; int rollno;
data-type member3; char name[20];
--------------------------------- float per;
---------------------------------- };
data-type member n;
};
Union Definition:-
Syntax:- union union-name var1,var2,,................,var n;
Ex:- union StudentDetails Praveen;
12
Output:
32 naveen 93.75
13
Table:
X Y X&Y
0 0 0
0 1 0
1 0 0
1 1 1
2) BIT-WISE OR ( | ):-
Table:
X Y X|Y
0 0 0
0 1 1
1 0 1
1 1 1
14
Table:
X Y X^Y
0 0 0
0 1 1
1 0 1
1 1 0
Syntax : ~ operand;
Table:
X ~X
0 1
1 0
5)SHIFT LEFT(<<): it is used to shift the no.of bits of an integer towards left. The vacant
positions on the right of the integer would be filled with zeros.
Syntax:
Operand<<n; where ‘n’ is the no .of bits of operand to be shifted towards left .
15
Example:
int x=5;
6)SHIFT RIGHT (>>):it is used to shift the no .of bits of an integer towards right. The
vacant positions on the left of the integer would be filled with zeros.
Syntax:
Operand>>n:, where ‘n’ is the no.of bits of operand to be shifted towards right.
void display(int n)
{
int i ,j,k;
for(i=15;i>=0;i--)
{
J=1<<i
k=j&n;
if(k>0)
printf(“1”);
else
printf(“0”);
}
}
Output
Enter x and y values
5 and 6
Enter the no.of bits to be shifted
5
x=0000 0000 0000 0101
y=0000 0000 0000 0110
xory=0000 0000 0000 0111
xandy=0000 0000 0000 0100
xexory=0000 0000 0000 0011
xlshift=0000 0000 0101 0000
xrshift=0000 0000 0000 0000