Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
How to Initialize Array of Structs in C?
Next article icon

Array of Structures in C

Last Updated : 23 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

An array of structures is simply an array where each element is a structure. It allows you to store several structures of the same type in a single array.

Let's take a look at an example:

C
#include <stdio.h> #include <string.h>  // Structure definition struct A {     int var; };  int main() {        // Declare an array of structures     struct A arr[2];    	arr[0].var = 10;     arr[1].var = 20;      for (int i = 0; i < 2; i++)         printf("%d\n", arr[i].var);      return 0; } 

Output
10 20 

Explanation: We define a Person structure with name and age as members. An array of 2 Person structures is declared, and each element is populated with different people’s details. A for loop is used to iterate through the array and print each person's information.

Table of Content

  • Array of Structure Declaration
  • Need for Array of Structures
  • Basic Operations on Array of Structures
    • Initialization
    • Access and Update Members
    • Traversal
  • Example of Array of Structure
    • Store Student Information
    • Find the Size of Array of Structures

Array of Structure Declaration

Once you have already defined structure, the array of structure can be defined in a similar way as any other variable.

struct struct_name arr_name [size];

Need for Array of Structures

Suppose we have 50 employees, and we need to store the data of 50 employees. So for that, we need to define 50 variables of struct Employee type and store the data within that. However, declaring and handling the 50 variables is not an easy task. Let's imagine a bigger scenario, like 1000 employees.

So, if we declare the variable this way, it's not possible to handle this.

struct Employee emp1, emp2, emp3, .. . ... . .. ... emp1000;

For that, we can define an array whose data type will be struct Employee soo that will be easily manageable.

  • Arrays of structures allow you to group related data together and handle multiple instances of that data more efficiently.
  • It is particularly useful when you want to manage large datasets (such as a list of employees, students, or products) where each element has a consistent structure.

Basic Operations on Array of Structures

Following are the basic operations on array of structures:

Initialization

A structure can be initialized using initializer list and so can be the array. Therefore, we can initialize the array of structures using nested initializer list:

struct struct_name arr_name [size] = {
{element1_value1, element1_value2, ....},
{element2_value1, element2_value2, ....},
......
......
};

We can also skip the nested braces, but it is not recommended as we can easily lose the count of the element/member and may mess up the initialization.

struct struct_name arr_name [size]= {
element1_value1, element1_value2 ....,
element2_value1, element2_value2 .....
};

GNU C compilers support designated initialization for structures. So we can also use this in the initialization of an array of structures:

struct struct_name arr_name [size] = {
{.element3 = value, .element1 = value, ....},
{.element2 = value, .elementN = value, ....},
......
......
};

Let's take a look at an example of all these initializations:

C
//Driver Code Starts{ #include <stdio.h> #include <string.h>  //Driver Code Ends }  // Structure definition struct A {     int var;   	char c; };  int main() {        // Declaration and initialization using nested   	// initializer list     struct A arr1[2] = { {1, 'a'}, {2, 'b'} };        // Declaration and initialization using non-nested   	// initializer list   	struct A arr2[2] = { 10, 'A', 20, 'B' };    	// Designated initialization   	struct A arr3[2] = { {.c = 'A', .var = 10},                         {.var = 2, .c = 'b'} };  //Driver Code Starts{      for (int i = 0; i < 2; i++)         printf("%d %c ", arr1[i].var, arr1[i].c);     printf(" ");      	for (int i = 0; i < 2; i++)         printf("%d %c ", arr2[i].var, arr2[i].c);     printf(" ");      	for (int i = 0; i < 2; i++)         printf("%d %c ", arr3[i].var, arr3[i].c);     printf(" ");      return 0; }  //Driver Code Ends } 

Output
1 a 2 b  10 A 20 B  10 A 2 b  

Access and Update Members

To access the members of a structure in an array, use the index of the array element followed by the dot (.) operator. The general syntax is:

arr_name[index].member_name

where,

  • arr_name: The name of the array of structures.
  • index: The index of the structure element in the array.
  • member_name: The member within the structure you want to access.
C
//Driver Code Starts{ #include <stdio.h> #include <string.h>  // Structure definition struct A {     int var;   	char c; };  int main() {     struct A arr[2] = { {1, 'a'}, {2, 'b'} };      	 	// Access the member c of second element of arr //Driver Code Ends }      printf("%c ",arr[1].c);      	// Update the value and access again   	arr[1].c = 'Z';   	printf("%c",arr[1].c);  //Driver Code Starts{        return 0; }  //Driver Code Ends } 

Output
b Z

Traversal

The array of structure can be easily traversed in the same way we traverse the array.

C
#include <stdio.h> #include <string.h>  // Structure definition struct A {     int var;   	char c; };  int main() {     struct A arr[5] = { {1, 'a'}, {2, 'b'}, {3, 'c'},                        {4, 'd'}, {5, 'e'} };      	// Traverse arr   	for (int i = 0; i < 5; i++)       	printf("%d %c\n", arr[i].var, arr[i].c);      return 0; } 

Output
1 a 2 b 3 c 4 d 5 e 

Example of Array of Structure

The below code demonstrates the application of array of structure inside a C program:

Store Student Information

C
#include <stdio.h> #include <string.h>  // Structure definition struct Student {     char name[50];     int age;     float marks; };  int main() {        // Declaration and initialization of an array of structures     struct Student students[3] = {         {"Nikhil", 20, 85.5},         {"Shubham", 22, 90.0},         {"Vivek", 25, 78.0}     };      // Traversing through the array of structures and displaying the data     for (int i = 0; i < 3; i++) {         printf("Student %d:\n", i+1);         printf("Name: %s\n", students[i].name);         printf("Age: %d\n", students[i].age);         printf("Marks: %.2f\n\n", students[i].marks);     }      return 0; } 

Output
Student 1: Name: Nikhil Age: 20 Marks: 85.50  Student 2: Name: Shubham Age: 22 Marks: 90.00  Student 3: Name: Vivek Age: 25 Marks: 78.00  

Find the Size of Array of Structures

The array of structures are also affected by the concept called structure padding.

C++
#include <stdio.h> #include <string.h>  struct Student {     char name[50];     int age;     float marks; };  int main() {     struct Student students[3] = {         {"John", 20, 85.5},         {"Alice", 22, 90.0},         {"Bob", 25, 78.0}     };      // Printing size of students     printf("%ld", sizeof(students));      return 0; } 

Output
180



Next Article
How to Initialize Array of Structs in C?
author
maityamit_2003
Improve
Article Tags :
  • C Programs
  • Arrays
  • c-array
Practice Tags :
  • Arrays

Similar Reads

  • Array within Structure in C
    In C, a structure is a user-defined data type that allows us to combine data of different data types. While an array is a collection of elements of the same type. In this article, we will discuss the concept of an array that is declared as a member of the structure. Array within a Structure An array
    3 min read
  • How to Access Array of Structure in C?
    In C, we can create an array whose elements are of struct type. In this article, we will learn how to access an array of structures in C. For Example, Input:myArrayOfStructs = {{'a', 10}, {'b', 20}, {'A', 9}}Output:Integer Member at index 1: 20Accessing Array of Structure Members in CWe can access t
    2 min read
  • How to Create an Array of Structs in C?
    In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of
    2 min read
  • Return an Array in C
    In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C
    6 min read
  • How to Initialize Array of Structs in C?
    In C, arrays are data structures that store the data in contiguous memory locations. While structs are used to create user-defined data types. In this article, we will learn how to initialize an array of structs in C. Initializing Array of Structures in CWe can initialize the array of structures usi
    2 min read
  • Array of Pointers to Strings in C
    In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings
    2 min read
  • How to Search in Array of Struct in C?
    In C, a struct (short for structure) is a user-defined data type that allows us to combine data items of different kinds. An array of structs is an array in which each element is of struct type. In this article, we will learn how to search for a specific element in an array of structs. Example: Inpu
    2 min read
  • How to Pass Array of Structure to a Function in C?
    An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C. Passin
    2 min read
  • How to Create a Dynamic Array of Structs?
    In C, we have dynamic arrays in which we can allocate an array of elements whose size is determined during runtime. In this article, we will learn how to create a dynamic array of structures in C. Create a Dynamic Array of Structs in CA dynamic array of structs in C combines dynamic arrays and struc
    2 min read
  • How to Iterate Through Array of Structs in C?
    In C, while working with an array of structs we have to iterate through each struct in an array to perform certain operations or access its members. In this article, we will learn how to iterate through an array of the structs in C. Iterate Over of an Array of StructuresTo iterate through an array o
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences