Structures May15
Structures May15
By
Prof. Ramesh Kumar Mohapatra
Dept. of CSE, NIT Rourkela
Introduction
A structure is a user defined data type for handling a group of
logically related data of different types.
Array is the collection of data of same type.
Keyword name
Syntax:- struct tag_name
{
data_type var1;
data_type var2;
. members/
. elements
.
};
Above declaration gives a format called template to represent information
Definition and Declaration
Defining a structure
struct book_bank
{
char title[20];
char author[15]; No space is reserved in the memory
int pages; until variable declaration
float price;
};
• Declaring a structure variable
struct book_bank book1,book2,book3;
Types of Structure Variable
Declaration
Type 1 Type 2
struct book _ bank struct book _ bank
{ {
char title[20]; char title[20];
char author[15];
int pages;
char author[15]; float price;
};
int pages;
float price; struct book _ bank
} book1,book2,book3; book1,book2,book3;
C Initializing structure
• Way 1 : Declare and Initialize
struct student {
char name[20];
int roll;
float marks;
}std1 = { "Pritesh",67,78.3 };
• Way 2 : Declaring and Initializing Multiple Variables
struct student {
char name[20];
int roll;
float marks;
};
struct student std1 = {"Pritesh",67,78.3};
struct student std2 = {"Don",62,71.3};
• Way 3 : Initializing inside main
struct student {
int mark1;
int mark2;
int mark3;
};
void main() {
struct student s1 = {89,54,65};
- - - - -- - - -
- -- - - - - --
}
Accessing Structure Members
• The link between a member and a variable is established using the
member operator ‘.’ also known as dot operator.
#include<stdio.h> Output:
Vehicle No of Wheels : 4
struct Vehicle { Vehicle Name : Nano
int wheels; Vehicle Color : Red
char vname[20];
char color[10];
}v1 = {4,"Nano","Red"};
int main()
{
printf("Vehicle No of Wheels : %d\n",v1.wheels);
printf("Vehicle Name : %s\n",v1.vname);
printf("Vehicle Color : %s\n",v1.color);
return 0;
}
Structure Initialization
A structure variable can be initialized as other
variables.
• First method: 2 bytes 60
struct st_record
{
180.45
4 bytes
int weight;
float height;
};
main()
{
struct st_record student2={67,145.78};
………
………
}
Array of Structures
struct marks
{
int mark1;
int mark2;
int mark3;
};
main()
{
struct marks student[2] ={{45,67,89},{56,87,98}};
…………
}
Array of Structure Inside
Memory
Student[0]. mark1 45
student[0].num[0] 60
struct marks
student[0].num[1] 80
{
student[0].num[2] 90
int num[3];
student[0].name A
char name;
student[1].num[0] 60
} student[2];
student[1].num[1] 75
student[1].num[2] 89
student[1].name B
Example 3: Array of Structures
#include <stdio.h> printf("Enter the name, id, and marks of
struct student { student3: ");
char name[20]; scanf(“%s %d %f",s3.name,&s3.id,&s3.marks);
int id;
float marks; printf(“Printing the Details ------------ ");
}; printf (“%s %d %f \
void main() n",s1.name,&s1.id,&s1.marks);
{ printf (“%s %d %f \
n",s2.name,&s2.id,&s2.marks);
struct student s1,s2,s3;
printf (“%s %d %f \
printf("Enter the name, id, and marks of
student1: "); n",s3.name,&s3.id,&s3.marks);
scanf(“%s %d %f",s1.name,&s1.id,&s1.marks); }
printf("Enter the name, id, and marks of
student2: ");
scanf(“%s %d %f",s2.name,&s2.id,&s2.marks);
Nesting of Structures
struct salary struct salary
{ {
char name; char name;
char department; char department;
int basic_pay; int basic_pay;
int dearness_allowance ; struct
int house_rent_alowan; {
int city_allowance; int dearness ;
int house_rent;
int city;
} employee; } allowance;
} employee;
struct Employee {
char ename[20];
int ssn;
float salary;
struct date
{
int date;
int month;
int year;
}doj;
}emp1;
Accessing Month Field : emp1.doj.month
Accessing day Field : emp1.doj.day
Accessing year Field : emp1.doj.year
Example 4: Nested Structure
#include <stdio.h>
main() {
struct Employee {
printf("\nEmployee Name :
char ename[20]; %s",emp.ename);
int ssn; printf("\nEmployee SSN :
float salary; %d",emp.ssn);
struct date { printf("\nEmployee Salary :
int date; %f",emp.salary);
int month;
printf("\nEmployee DOJ :
int year;
%d/%d/%d",
}doj;
emp.doj.date,emp.doj.month,e
}emp = mp.doj.year);
{"Pritesh",1000,1000.50,
}
{15,5,2023}};
Assignment
1. WAP to add two distances in the inch-feet system.
E.g. Enter distance as a structure with int feet and float inch variables.
2. WAP to read the details of two workers and calculate total payment of each worker
using structure. It contains name, wage and no. of working days.
3. WAP to accept batting information of cricket team using structure. It contains player
name and runs scored by players. Calculate total runs scored by cricket team.
4. WAP to read information of an employee. It contains Name, Empcode, Birthday and
Joining date. Calculate age of an employee at the time of joining.
5. WAP to calculate the percentage of marks. It contains rollno, name and marks.