19CSE102
Computer Programming
Array of structure and
Nested Structure
List of Topics
• Basics of Structure –Typedef
• Array of Structure
• Nested Structure
• Pointer to Structure
Another note about structs
• The following is not legal:–
struct motor {
float volts;
float amps;
float rpm;
unsigned int phases;
}; //struct motor
You must write
motor m;
struct motor m;
motor *p;
struct motor *p;
3
Typedef
• Typedef is a keyword that is used to give a new
symbolic name for the existing name in a C program.
This is same like defining alias for the commands.
E.g.,
typedef struct motor Motor;
E.g., typedef, lets you leave
Motor m, n; out the word “struct”
4
Declaring Structure Types (1/2)
• Syntax of the structure type:
typedef struct{
type1 id1;
type2 id2;
…
} struct_type;
• E.g.,
typedef struct{
char[20] name;
int age;
} student_info;
Example
An alternative way for structure
Consider the below structure. declaration using typedef in C:
Struct student typedef struct student
{ {
int mark [2]; int mark [2];
char name [10]; char name [10];
float average; float average;
} } status;
Copying and Comparing Structure Variables
• Two variables of the same structure type can be copied in the same
way as ordinary variables.
• If student1 and student2 belong to the same structure, then the
following statements are valid:
student1=student2;
student2=student1;
• However, the statements such as:
student1==student2
student1!=student2 are not permitted.
• If we need to compare the structure variables, we may do so by
comparing members individually.
Array of structure
• consider a structure as:
struct student
{
char name[20];
int roll;
char remarks;
float marks;
};
• If we want to keep record of 100 students, we have to make
100 structure variables like st1, st2, …,st100.
• This situation we can use array of structure to store the records
of 100 students which is easier and efficient to handle by loop.
Array of structure
Two ways to declare an array of structure:
Sample 1 Sample 2
struct student { struct student
{
char name[20]; char name[20];
int roll; int roll;
float marks; float marks;
};
}st[100];
struct student st[100];
Example
#include <stdio.h> // 3rd student's record
#include <string.h>
struct student record[2].roll=3;
{ int roll; strcpy(record[2].name, "Chandran");
char name[20]; record[2].marks = 81.5;
float marks;
};
for(i=0;i<3;i++)
int main()
{ {
struct student record[3]; printf(" Records of STUDENT : %d \n", i+1);
int i;
// 1st student's record printf(" Id is: %d \n", record[i].roll);
record[0].roll=1; printf(" Name is: %s \n", record[i].name);
strcpy(record[0].name, "Aakash");
record[0].marks = 86.5; printf(" Percentage is: %f\n\n",record[i].marks);
}
// 2nd student's record return 0;
record[1].roll=2;
strcpy(record[1].name, "Saravanan"); }
record[1].marks = 90.5;
Array within Structure
• We can use single or multi dimensional arrays of type
int or float.
• E.g.
struct student
{
int roll;
float marks[6];
};
struct student s[100];
Array within Structure
• Here, the member marks contains six elements,
marks[0], marks[1], …, marks[5] indicating marks
obtained in six different subjects.
• These elements can be accessed using appropriate
subscripts.
• For example, s[25].marks[3] refers to the marks
obtained in the fourth subject by the 26th student.
Example
Nested Structure- Structure within another
Structure
• Consider a structure personal_record to store the
information of a person as:
struct personal_record
{
char name[20];
int day_of_birth;
int month_of_birth;
int year_of_birth;
float salary;
}person;
Nested Structure
• Group all the items related to birthday together and declare them
under a substructure as:
struct Date
{
int day_of_birth;
int month_of_birth;
int year_of_birth;
};
struct personal_record
{
char name[20];
struct Date birthday;
float salary;
}person;
Nested Structure
• The structure personal_record contains a member named birthday
which itself is a structure with 3 members. This is called structure
within structure.
• The members contained within the inner structure can be accessed
as:
person.birthday.day_of_birth
person.birthday.month_of_birth
person.birthday. year_of_birth
• Other members within the structure personal_record are accessed
as usual:
person.name
person.salary
Structure Nested Structure
printf("Enter name:\t"); printf("\nEnter day of birthday:\t");
scanf("%d", &person.birthday.day_of_birth);
scanf("%s", person.name);
printf("\nEnter month of birthday:\t");
printf("\nEnter salary:\t"); scanf("%d",&person.birthday.month_of_birth);
scanf("%f", &person.salary); printf("\nEnter year of birthday:\t");
scanf("%d", &person.birthday.year_of_birth);
Nested Structure
• More than one type of structures
can be nested The members
contained within the inner
structure can be accessed as: struct personal_record {
struct date {
int day;
float salary;
int month; struct date birthday;
int year; struct name full_name;
};
};
struct name {
char first_name[10];
char middle_name[10];
char last_name[10];
};
References
• Byron Gottfried. Programming With C. Fourth Edition,
McGrawHill,; 2018.
• Brian W. Kernighan and Dennis M. Ritchie. The C Programming
Language. Second Edition, Prentice Hall, 1988.
• Eric S. Roberts. Art and Science of C. Addison Wesley; 1995.
• Jeri Hanly and Elliot Koffman. Problem Solving and Program
Design in C. Fifth Edition, Addison Wesley (Pearson); 2007.
• Balagurusamy,Computing Fundamentals & C Programming,
Tata McGraw-Hill,2008
Thank you ……