Categories: C

Array of Structure

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

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago