Array of Structure:
In C, an array of structures is a collection of many structure variables, each of which has information on a different entity. An array of structures is used in C to store information about several entities of various data types. The collection of structures is another name for the array of structures.
Example:
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Output:
Enter Records of 5 students
Enter Rollno:1
Enter Name:Coderzpy
Enter Rollno:2
Enter Name:Rabecca
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Sarfraz
Student Information List:
Rollno:1, Name:Coderzpy
Rollno:2, Name:Rabecca
Rollno:3, Name:Vimal
Rollno:4, Name:James
Rollno:5, Name:Sarfraz
Let us now explore the intricacies of structures with a view of programming convenience.
Additional features of Structures:
- Using the assignment operator, the values of a structure can be assigned to another structure variable of the same type.
- One structure can be nested inside another. Complex data types can be generated using this feature.
- A structural variable can be provided into a function as well. We have the option of passing particular structure elements or the entire structure variable at once.
- In the same way that we may have a pointer pointing to an int or a pointer referring to a char, we can also have a pointer pointing to a struct. These are referred to as ‘structure pointers’.
- It should be noted that structure elements are stored in adjacent memory locations.
Uses of Structures:
Structures can be used for a variety of purposes:
- Changing the size of the cursor.
- Clearing the contents of the screen.
- Placing the cursor at an appropriate place on the screen.
- Drawing any shape on the screen.
- Obtaining a key from the keyboard.
- Checking the memory size of the computer.
- etc.
Note: also read about arrays in C & Structure in C
Follow Me
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment