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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago