0% found this document useful (0 votes)
3 views20 pages

L32-L33-Array of Structures

The document provides an overview of structures in programming, including definitions, initialization, and operations on structures. It covers concepts such as arrays of structures, pointers to structures, and nesting structures, along with examples and programming exercises. Additionally, it discusses the use of structures in functions and provides sample code for practical understanding.

Uploaded by

khushpatel1222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

L32-L33-Array of Structures

The document provides an overview of structures in programming, including definitions, initialization, and operations on structures. It covers concepts such as arrays of structures, pointers to structures, and nesting structures, along with examples and programming exercises. Additionally, it discusses the use of structures in functions and provides sample code for practical understanding.

Uploaded by

khushpatel1222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Array of Structures & Pointers to Structures

Structures: overview
▪ Definition & structure ▪ Giving values to members
variable declaration Using dot operator ‘.’
struct student s1. rollno = 25;
{ int rollno; cin>>s1.name;
int age;
char name[20]; ‘.’ operator acts as Link between
}s1, s2, s3; member and a Structure variable.

▪ Initialization ▪ Assign & compare members


int main( ){ s1 = s2 ; assignment (allowed)
struct ----------------------------
{ int rollno; s1 == s2 comparison (not allowed)
int age; s1!=s2 comparison (not allowed)
}stud={20, 21};
… ----------------------------------
… s1.rollno == s2.rollno; (allowed)
return 0; s1.rollno!=s2.rollno; (allowed)
}
04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 2
Objectives

•To learn and appreciate the following concept

• Array of structures

• Pointers and Structures

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 3


Session outcome

• At the end of session one will be able to

• Understand the overall ideology of array of structures


• Write programs using array of structures
• Understand the concept of pointers to structures
• Write programs on pointers to structures.

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 4


Arrays of structures
• An ordinary array: One type of data

0 1 2 … 98 99

• An array of structs: Multiple types of data in each array


element.

0 1 2 … 98 99

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 5


Array of structures
We can define single or multidimensional arrays as structure variables.
struct marks
{
int subject1;
int subject2;
int subject3;
} ;
marks student[80];

▪ Defines an array called student, that consists of 80 elements.

▪ Each element is defined to be the type marks.

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 6


Array of structures – Initialization
struct marks {
int subject1; Memory
int subject2; student[0].subject1 45
int subject3; student[0].subject2 47
} ; student[0].subject3 49

student[1].subject1 43
main(){ student[1].subject2 44
marks student[]={
student[1].subject3 45
{45,47,49},
student[2].subject1 46
{43,44,45},
student[2].subject2 42
{46,42,43}
}; student[2].subject3 43

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 7


Array of Structure: Example
struct Book { //Structure Definition
char title[20];
char author[15];
int pages;
float price;
};
int main( ){
struct Book b[10];
printf("Input values");
for (int i=0;i<3;i++)
scanf("%s %s %d %f", b[i].title, b[i].author, &b[i].pages, &b[i].price);
for (int j=0;j<3;j++)
printf("%s\t %s\t %d\t %f\n", b[j].title, b[j].author, b[j].pages, b[j].price);
return 0;
}
04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 8
Arrays within Structures
We can define single or multidimensional arrays inside a structure.
struct marks
{ int rollno;
float subject[3];
} student[2] ;

The member subject contains 3 elements; subject[0], subject[1] & subject[2].

student[1].subject[2];
▪ Refers to the marks obtained in the third subject by the second student.

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 9


Arrays within structures : example
#include<stdio.h>
int main(){ //Structure Definition
struct marks student[3] ={{0,45,47,49}, struct marks{
{0,43,44,45}, int total;
{0,46,42,43}}; int sub[3];
int i, j ; };
//students total
for(i=0;i<=2;i++) {
for(j=0;j<=2;j++)
student[i].total+=student[i].sub[j]; }
printf("Grand Total of each student:");

for(i=0;i<=2;i++)
printf("\nTotal of student[%d]= %d", i, student[i].total);
return 0;
}

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 10


Structures within Structures
Structure within structure means nesting of structures.
for instance see the following structure defined to store information about
students
struct student{
int rollno;
char name[15];
struct { // marks for 3 subjects under structure marks
int sub1;
int sub2;
int sub3;
}marks;
}fs[3]; //3 students

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 11


Structures within Structures
//Structure Definition
//Structure Definition struct m{
int sub1;
struct student{ int sub2;
int rollno; int sub3;
char name[15]; };
struct m marks;
Tag name is used to define
}fs[3]; inner structure marks

The members contained in the inner structure namely sub1, sub2


and sub3 can be referred to as:
fs[i].marks.sub1;
fs[i].marks.sub2;
fs[i].marks.sub3;

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 12


Structures and functions
void read(struct book x[]); // prototype //Structure
int main() { Definition
int i; struct book
struct book b1[2]; {
int ibn;
printf("\n Enter IBN, Author name & Price \n"); char author[15];
read(b1); // function call float price;
};
printf("\nThe book details entered are:\n");
for(i=0;i<2;i++){ //function definition
printf("\n Book %d", i+1); void read(struct book a[])
printf("\nIBN: \t\t%d", b1[i].ibn); {
printf("\nAuthor: \t%s", b1[i].author); int i;
for(i=0;i<2;i++){
printf("\nPrice: \t\t%f", b1[i].price); printf("\nBook %d\n", i+1);
} scanf("%d", &a[i].ibn);
return 0; scanf("%s", a[i].author);
scanf("%f", &a[i].price);
} }
}
04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 13
Structures -Problems

Write programs to

1. Create a student record with name, rollno, marks of 3 subjects (m1, m2, m3).
Compute the average of marks for 3 students and display the names of the
students in ascending order of their average marks.

2. Create an employee record with emp-no, name, age, date-of-joining (year),


and salary. If there is 20% hike on salary per annum, compute the retirement
year of each employee and the salary at that time. [standard age of retirement
is 55]

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 14


Structures – Solution for Q1
int main() struct student{
{ int rollno;
struct student temp, fs[3] =
char name[15];
{{1,"manish",45,47,49},
{2,"ankur",43,44,45}, struct {
{3,"swati",46,42,43}}; int sub1;
int i, n=3, total[3]={0}, avg[3]={0},tot=0; int sub2;
int sub3;
for(i=0; i< n; i++) {
}marks;
total[i]=fs[i].marks.sub1+fs[i].marks.sub2+
fs[i].marks.sub3; //students total };
avg[i] = total[i]/3;
}
//display
printf("Total & Average of each student.\n“);
for(i=0;i<n;i++){
printf("\nTotal of %s = %d & avg = %d", fs[i].name, total[i], avg[i]);
}
04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 15
Structures – Solution for Q1
// sorting
for(i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(avg[i] > avg[j])
{
temp=fs[i]; //Swapping
fs[i]=fs[j];
fs[j]=temp;
}
for(i=0;i<n;i++) //Sorted list w.r.to average marks
printf("\n%s\n",fs[i].name);
return 0;
} //end of main

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 16


Pointers and structures
Consider the following structure
struct inventory {
char name[30];
int number;
float price;
} product[2],*ptr;

This statement declares product as an array of 2 elements, each of the type struct
inventory.

ptr=product; assigns the address of the zeroth element of product to ptr or ptr
points to product[0];

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 17


Pointers and Structures
Its members are accessed using the following notation
ptr →name
ptr →number
ptr →price
The symbol → is called arrow operator (also known as member selection
operator)

When ptr is incremented by one, it points to the next record. i.e. product[1]

The member price can also be accessed using


(*ptr).price
Parentheses is required because ‘.’ has higher precedence than the operator *

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 18


Pointers and Structures- example
struct invent{
int main() {
char name[30]; struct invent prod[3], *ptr;
int number; printf("Enter 3 (0, 1 and 2 )sets of Name,
float price; Number and Price");
}; for(ptr = prod; ptr < prod+3; ptr++)
scanf("%s %d %f",ptr ->name, &ptr ->number, &ptr ->price);

ptr=prod;

while(ptr < prod+3) {


printf("%s %d %f\n", ptr ->name,
ptr ->number, ptr ->price); ptr++;
}
return 0;
}
04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 19
Summary

• Array of Structures

• Arrays within Structures

• Structures within Structures

• Structures and Functions

• Pointers and Structures

04/03/2023 CSE 1071 Problem Solving using Computers (PSUC) - 2022 20

You might also like