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 structstruct StructName { dataType1 member1; dataType2 member2; // more members...};// Declare an array of structsStructName 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 StructsThe 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; } OutputArray 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) Comment More infoAdvertise with us Next Article How to Create an Array of Structs in C++? P pratyusujhr Follow Improve Article Tags : C++ Programs C++ cpp-array cpp-struct CPP Examples +1 More 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 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 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 How to Create a Stack of Set in C++? In C++ STL, Stacks are a type of container adaptor with 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. Sets are a type of associative container in which each element is unique and in some sorted order. In this arti 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read Like