FALLSEM2024-25_CSE2010_ETH_VL2024250102099_2024-09-02_Reference-Material-I.pdf
Document Details
2023
Tags
Full Transcript
Module 1 CSE2010 2023-2024 June-December DR. ANINDITA KUNDU Dr. Anindita Kundu 9836864321 [email protected] PRP 315 A&B - 17 Lecture 11 Structures What is a structure? User defined data type that allows us to combine data types of different kinds Used to r...
Module 1 CSE2010 2023-2024 June-December DR. ANINDITA KUNDU Dr. Anindita Kundu 9836864321 [email protected] PRP 315 A&B - 17 Lecture 11 Structures What is a structure? User defined data type that allows us to combine data types of different kinds Used to represent a record Anything common noun can be considered as a record and hence structure Entities that define the characteristics of the record can be considered as structure members What is a structure? Example: Book is common noun, hence can be considered as a structure Entities that define a book are: Title, Author, Publisher, Volume, etc Book is the structure while Title, Author, Publisher, Volume, etc can be considered as structure members Why do we need it? Consider storing information of 1 book. 4 char arrays of size n can do the job Consider storing information of 5 book. 4, 5 x n char arrays can do the job Consider storing information of 10,000 book. Structure comes to rescue What does a structure look like? struct book{ char title; char author; char publisher; char volume; }; System defined data type : User defined data type float : struct book Declaring Structure Method 1 variables Method 2 struct student{ struct student{ int roll; int roll; char name; char name; int marks; int marks; }s1,s2,s3; }; int main(){ struct student s1,s2,s3; … return 0; } Example 1 int main(){ struct student{ struct student a; int roll; printf("\nEnter roll: "); char name; scanf("%d",&a.roll); }; fflush(stdin); printf("\nEnter name: "); gets(a.name); printf("\nName:%s,\nRoll: %d",a.name,a.roll); } Example 2 int main(){ struct student a, b; printf("\nEnter roll: "); scanf("%d",&a.roll); fflush(stdin); printf("\nEnter name: "); gets(a.name); b = a; printf("\nName:%s,\nRoll:%d",b.name,b.roll); } Example 2 int main(){ struct student a, b; What about 800 printf("\nEnter roll: "); students? scanf("%d",&a.roll); fflush(stdin); printf("\nEnter name: "); gets(a.name); b = a; printf("\nName:%s,\nRoll:%d",b.name,b.roll); } Array of Structures struct student{ s int roll; char name; int marks; 0 1 799 }; int main(){ struct student s; int arr; … return 0; } Storing data in. Structure variables Using dot(.) operator: used for accessing structure member via structure variable For single variables For array of structures Using arrow(->) operator: used for accessing structure member via pointer to structure or address of structure variable For single variables For array of structures Access structure members via single variable #include int main(){ #define sem 8 struct student s1; #define sub_per_sem 5 int i,j; struct student{ printf("\nEnter roll: "); int roll; char name; scanf("%d",&s1.roll); int marks[sem][sub_per_sem];fflush(stdin); }; printf("\nEnter name: "); scanf("%s",&s1.name); Access structure members via single variable for(i=0;i