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
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How to Create a Vector of Arrays in C++?
Next article icon

How to Create an Array of Structs in C++?

Last Updated : 11 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, a struct is a user-defined data type that allows us to combine data of different types and an array of structs is an array in which each element is of the struct type. In this article, we will learn how to create an array of structs in C++.

Creating an Array of Structs in C++

To create an array of structs, we first need to define the struct type and then declare an array of that struct using the below syntax.

Syntax to Create an Array of Structs in C++

// Define the struct
struct StructName {
dataType1 member1;
dataType2 member2;
// more members...
};// Declare an array of structs
StructName arrayName[arraySize];

Here,

  • StructName is the name of the struct.
  • dataType1, dataType2 are the data types of the members of the struct.
  • member1, member2 are the names of the members of the struct.
  • arrayName is the name of the array of structs.
  • arraySize is the size of the array.

C++ Program to Create an Array of Structs

The below program demonstrates how we can create an array of structs in C++.

C++
// C++ program to illustrate how to create an array of // structs #include <iostream> #include <string> using namespace std;  // Defining the struct struct Student {     int id;     string name; };  int main() {     // Declaring the size of the array     int size = 3;      // Declaring an array of structs     Student myArray[size];      // Initializing data to structs present in the array     for (int i = 0; i < size; i++) {         myArray[i].id = i + 1;         myArray[i].name = "Student" + to_string(i + 1);     }      // Printing the data of structs present in the array     cout << "Array Elements:" << endl;     for (int i = 0; i < size; i++) {         cout << "Element " << i + 1              << ": ID = " << myArray[i].id              << ", Name = " << myArray[i].name << endl;     }      return 0; } 

Output
Array Elements:  Element 1: ID = 1, Name = Student1  Element 2: ID = 2, Name = Student2  Element 3: ID = 3, Name = Student3  

Time Complexity: O(N), here N is the number of elements present in the array.
Auxiliary Space: O(N)




Next Article
How to Create a Vector of Arrays in C++?

P

pratyusujhr
Improve
Article Tags :
  • C++ Programs
  • C++
  • cpp-array
  • cpp-struct
  • CPP Examples
Practice Tags :
  • CPP

Similar Reads

  • How to Create a Stack of Arrays in C++?
    In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar
    2 min read
  • How to Create a Set of Arrays in C++?
    In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w
    2 min read
  • How to Create a Vector of Arrays in C++?
    In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3
    2 min read
  • How to Create Array of Arrays in C++
    Arrays are basic C++ data structures that allow users to store the same data type in memory sequentially. To manage more complicated data structures, you may sometimes need to build an array of arrays, often called a 2D array or a matrix. In this article, we will learn how to create an array of arra
    3 min read
  • How to Create a Map of Arrays in C++?
    In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9};
    2 min read
  • How to Create a Set of Sets in C++?
    In C++, sets are STL containers that store unique elements of the same type in a sorted manner. Sets of sets, also known as nested sets, are collections in which each element of the outer set contains another set as its element. In this article, we will learn how to create a set of sets in C++. Set
    2 min read
  • How to Create a Deque of Arrays in C++?
    In C++, a deque (double-ended queue) is a data structure that allows insertion and deletion at both ends whereas arrays are fixed-size collections of elements. In this article, we will learn how to create a deque of arrays in C++ STL. Example: Input: myArray1 = {1, 4, 8, 9, 11} myArray2 = {1, 2, 3,
    2 min read
  • How to Create a Stack of Stack in C++?
    In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in
    2 min read
  • How to Create a Stack of Strings in C++?
    In C++, Stacks are a type of container adaptor with a LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn how to create a stack of strings in C++. Creating a Stack of Strings in C++To crea
    1 min read
  • How to Create a Stack of Map in C++?
    In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::map is an associative container that stores key-value pairs. In this article, we will learn how to create a stack of a map in C++. Example:Input: myMap = { {1: ‘a’}, {2: ‘b’}, {3: ‘c’} }; myMap = { {4
    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