3. A Structure can be defined to be a group of logically related data items,
which may be of different types, stored in contiguous memory locations,
sharing a common name, but distinguished by its members.
The decleration of a structure specifies the grouping of various data items into a
single unit without assigning any resources to them.
The syntax for declaring a structure in c is as follows
struct TAG
{
Data Type varaible-1;
Data Type variable-2;
……
……
Data Type variable-n;
};
Where,
struct is the keyword which tells the compiler that a structure is being defined.
Tag_name is the name of the structure.
variable1, variable2 … are called members of the structure.
The members are declared within curly braces.
The closing brace must end with the semicolon.
@2020 Presented By Y. N. D. Aravind
3
3
Example :
struct employee
{
int eno;
char name[80];
float sal;
};
4. Structure Definition
The decleration of a structure will not serve any purpose without its definition.
It only acts as a bluprint for the creation of variables of type struct. The
structure definition creates structure variable and allocates storage space for
them.
Method 1 : struct Structurename var1, var2, . . . . . ;
Example : struct employee x;
Example : struct employee x, y, z ;
Method 2 :
@2020 Presented By Y. N. D. Aravind
4
4
Example :
struct employee
{
int eno;
char name[80];
float sal;
};
2 bytes
80 bytes
4 bytes
Example :
struct employee
{
int eno;
char name[80];
float sal;
}x;
The structure variable can be
created during the decleration
as follows
5. Accessing Structure Members
@2020 Presented By Y. N. D. Aravind
5
The members of a structure can be accessed by using dot(.) operator.
dot (.) operator
Structures use a dot (.) operator(also called period operator or member operator) to
refer its elements. Before dot, there must always be a structure variable. After the dot,
there must always be a structure element.
syntax :- structure_variable_name . structure_member_name
struct student
{
char name [5];
int roll_number;
float avg;
};
struct student s1;
The members can be accessed using the variables as shown below,
s1.name --> refers the string name
s1.roll_number --> refers the roll_number
s1.avg --> refers avg
5
6. Structure Initilization
@2020 Presented By Y. N. D. Aravind
6
Like any other data types, structure variables can be initialized at the point of their
definition.
Consider the following structure decleration.
struct student
{
char name [5];
int roll_number;
float avg;
};
Example :-
A variable of the structure employee can be
initialized during its defination as folloes:
struct student s1 = {“Ravi”, 10, 67.8};
6
name
roll no
avg
7. Array of Structures
@2020 Presented By Y. N. D. Aravind
7
An array is a collection of elements of same data type that are stored in contiguous
memory locations. A structure is a collection of members of different data types stored in
contiguous memory locations. An array of structures is an array in which each element is
a structure. This concept is very helpful in representing multiple records of a file, where
each record is a collection of dissimilar data items.
As we have an array of integers, we can have an array of structures also. For example,
suppose we want to store the information of class of students, consisting of roll_number,
name and percentage, A better approach would be to use an array of structures.
Array of structures can be declared as follows, struct tag_name arrayofstructure[size];
Let‟s take an example, to store the information of 20 students, we can have the following
structure definition and declaration,
struct student
{
int rollno;
char name[80];
float per;
};
struct student s[20];
7
8. Program to illustrate an array of structures
@2020 Presented By Y. N. D. Aravind
8
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[80];
float per;
};
void main( )
{
struct student s[20];
int i,n;
clrscr();
printf("Enter no of students [ 1 – 20 ] n ");
scanf(“%d”,&n);
printf("Enter %d students details n ");
for(i = 0; i < n; i++)
scanf(“%d %s %f”,&s[i].rollno,s[i].name,&s[i].per);
printf("n Student details: n");
for(i=0; i<n; i++)
printf(“%3d %20s %9.2f n”, s[i].rollno,s[i].name,s[i].per);
getch(();
}
8
Input-Output
Enter no of students [ 1 – 20 ]
2
Enter 2 students details
1 Ramesh 90
2 Anusha 80
Student details:
1 Ramesh 90.00
2 Anusha 80 .00
9. Array Within Structures
@2020 Presented By Y. N. D. Aravind
9
It is also possible to declare an array as a member of structure, like declaring ordinary
variables. For example to store marks of a student in six subjects then we can have the
following definition of a structure.
Example :-
struct student
{
int rollno;
char name[80];
int marks[6];
};
Then the initialization of the array marks done as follows,
struct student s1= {“ravi”, 34, {60,70,80,68,90,75}};
The values of the member marks array are referred as follows,
s1.marks [0] --> will refer the 0th element in the marks 60
s1.marks [1] --> will refer the 1st element in the marks 70
s1.marks [2] --> will refer the 2nd element in the marks 80
s1.marks [3] --> will refer the 3rd element in the marks 68
s1.marks [4] --> will refer the 4th element in the marks 90
s1.marks [5] --> will refer the 5th element in the marks 75
9
10. To create a list of students details and display them.
@2020 Presented By Y. N. D. Aravind 10
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[80];
int marks[6];
int sum;
float per;
};
void main( )
{
struct student x[20];
int i,n,j;
clrscr();
printf("Enter no of students [ 1 – 20 ] n ");
scanf(“%d”,&n);
for(i = 0; i < n; i++)
{
printf("Enter rollno , name of students - %d n “,i+1);
scanf(“%d %s ”,&x[i].rollno,x[i].name);
Printf(“ Enter marks in six subjects n”):
for(j = 0; j < 6; j++)
scanf(“%d”, &x[i].marks[j]);
}
for(i = 0; i < n; i++)
{
x[i].sum = 0;
for(j = 0; j < 6; j++)
x[i].sum += x[i].marks[j];
x[i].per = (float) x[i].sum / 6;
}
printf(“ Roll no t Name t Percentage n”);
for(i=0; i < n; i++)
printf(“%3f %20s %9.2f n”,x[i].rollno,x[i].name,
x[i].per);
getch();
}
Input – Output
Enter no of students [ 1 – 20 ]
2
Enter rollno , name of students –1
1 Ramesh
Enter marks in six subjects
68 79 77 60 91 75
Enter rollno , name of students – 2
2 Anusha
Enter marks in six subjects
88 68 66 60 90 74
Roll no Name Percentage
1 Ramesh 75.00
2 Anusha 74.40
11. Structure Within Structure
@2020 Presented By Y. N. D. Aravind
11
A structure which includes another structure is called nested structure or structure within structure.
i.e a structure can be used as a member of another structure. There are two methods for declaration
of nested structures.
11
1. The syntax for the nesting of the structure is as
follows
struct tag_name1
{
type1 member1;
…….
…….
};
struct tag_name2
{
type1 member1;
……
……
struct tag_name1 var;
……
};
The syntax for accessing members of a nested
structure as follows,
outer_structure_variable .
inner_structure_variable . member_name
2. The syntax of another method for the nesting
of the structure is as follows
struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
- - - - - - - - - - -
- - - - - - - - - - -
<data-type> element n;
struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
- - - - - - - - - - -
- - - - - - - - - - -
<data-type> element n;
}inner_struct_var;
}outer_struct_var;
12. To illustrate the concept of structure within structure.
@2020 Presented By Y. N. D. Aravind 12
#include<stdio.h>
#include<conio.h>
Struct date
{
int day;
int month;
int year;
};
struct student
{
int rollno;
char name[80];
float per;
struct date doa;
};
void main( )
{
struct student x;
clrscr();
printf("Enter Roll no Name DOB and Per n ");
scanf(“%d %s %d %d %d %f”,&x.rollno,
x.name,&x.doa.day, &x.doa.month,
&x.doa.year,&x.per);
printf(“ Roll no : %d n”,x.rollno);
printf(“ Name : %s n”,x.name);
printf(“ Percentage =:%3.2f n”,x.per);
printf(“ DOB : %d / %d /%d n”,
x.doa.day,x.doa.month,x.doa.year);
getch();
}
Input – Output
Enter Rollno Name DOB and Per
1 Ramesh 20 10 2008 90
Roll no : 1
Name : Ramesh
Percentage : 90.00
DOB : 20/10/2008
13. Self Referential Structure
@2020 Presented By Y. N. D. Aravind
13
A structure contaning a pointer to the same type is referred to as self- referential structure. It is
useful for implementing data structures such as linked list, queues, stacks and trees etc.. A linked list
consists of structures related to each other through pointers. The self referential pointer in the
structure points to the next node of a list.
13
Syntax:-
struct tag_name
{
Type1 member1;
Type2 member2;
……….
struct tag_name *next;
};
Ex:-struct node
{
int data; node
struct node *next;
} n1, n2;
data next
14. UNION
@2020 Presented By Y. N. D. Aravind
14
A union is one of the derived data types. Union is a collection of variables referred under a single
name. The syntax, declaration and use of union is similar to the structure but its functionality is
different
14
Syntax:-
Union Union_name
{
Type1 member1;
Type2 member2;
……….
............
Typen membern;
}
Union is a keyword , Union name is any user defined name which should be a valid C identifier.Data
type is any valid data type supported by C or user-defined type. Member-1, member-2, ....,member-n
are the members of union.
The syntax of declaring a variable of union type is
Syntax:-
Union Union_name var1,var2,.......................
15. ENUMERATED DATA TYPE
@2020 Presented By Y. N. D. Aravind
15
Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names
to integral constants, the names make a program easy to read and maintain.
The syntax of defining an enumerated type is as follows:
The syntax of declaring a variable of enum type is similar to that used while declaring
variables of structure or union type
Example :-
enum boolean{true, false};
enum boolean t,f;
t= true;
f= false;
The compiler automatically assigns integer digits begining with 0 to all the enumerated
constants. „i.e‟ the enumerated constant true is assigned 0, false is assigned 1
15
Syntax:-
enum identifier{ value-1,varlue-2,......................value-n};
Syntax:-
enum identifier v1,v2,......................vn;
16. ENUMERATED DATA TYPE
@2020 Presented By Y. N. D. Aravind
16
However, the automatically assignments can be overridden by assigning values
explicitly to the enumerated constants.
enum State {Working = 1, Failed};
Here, the constant working is assigned the value of 1, the remaning constants are
assigned values that increase successively by 1.
16
#include<stdio.h>
#include<conio.h>
void main( )
{
enum month{ Jan, Feb, Mar, Apr, May, June, July, Aug, Sep, Oct, Nov, Dec};
enum month year_st, year_end;
clrscr();
year_st = Jan;
year_end = Dec;
printf(“Jan = %d n”, year_st);
printf(“ Feb = %d n “, year_end;
getch();
}
Input –Output
Jan = 0
Dec =11
17. Typedef
@2020 Presented By Y. N. D. Aravind
17
typedef is a keyword used in C language to assign alternative names to existing
datatypes. Its mostly used with user defined datatypes, when names of the datatypes
become slightly complicated to use in programs. Following is the general syntax for
using typedef,
typedef <existing_name> <alias_name>;
Lets take an example and see how typedef actually works.
typedef unsigned long ulong;
The above statement define a term ulong for an unsigned long datatype. Now
this ulong identifier can be used to define unsigned long type variables.
ulong i,
typedef can be used to give a name to user defined data type as well. Lets see its use with
structures.
typedef struct
{ type member1;
type member2;
type member3;
} type_name;
Here type_name represents the stucture definition associated with it. Now
this type_name can be used to declare a variable of this stucture type.
type_name t1, t2;
17
18. Thank You
@2020 Presented By Y. N. D. Aravind
18
Presented By
Y. N. D. ARAVIND
M.Tech, Dept of CSE
Newton’s Group Of Institutions, Macherla