Array of Structures vs Array within a Structure in C++
Last Updated :
08 Feb, 2024
In C++, an array of structure and an array within structure are used frequently used for data storage. Although they sound similar, they work pretty differently from each other. In this article, we will discuss the key differences between an array of structures and an array within structures and clarify the confusion.
Array of Structures in C++
An array of structures combines both an array and a structure to handle complex data neatly. Instead of creating many separate variables of structure, we use an array of structures. Each element in this array is a structure on its own and since the array elements are stored continuously in memory, it allows for quick access and makes our program more efficient and straightforward.
Syntax
structName charArrayName[size];
We can also create a multidimensional array to store more structures.
Example
The below example demonstrates the use of an array of structures in C++.
C++
#include <cstring>
#include <iostream>
using namespace std;
struct Coordinates {
int x;
int y;
};
int main()
{
int size = 3;
Coordinates vertices[size];
vertices[0].x = 2;
vertices[0].y = 4;
vertices[1].x = 3;
vertices[1].y = 6;
vertices[2].x = 4;
vertices[2].y = 8;
for ( int i = 0; i < size; ++i) {
cout << "Coordinates of ( x , y ) " << i + 1
<< ": (" << vertices[i].x << ", "
<< vertices[i].y << ")" << endl;
}
return 0;
}
|
Output
Coordinates of ( x , y ) 1: (2, 4)
Coordinates of ( x , y ) 2: (3, 6)
Coordinates of ( x , y ) 3: (4, 8)
Array Within a Structure in C++
An array within a structure simply means that we can create one or more arrays inside a structure as structure member which can be useful when we want to associate a collection of items within a single structure.
Syntax
structName myVar; // Creates a single variable of structName
Example
The below example demonstrates the use of an array within the structure.
C++
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int grades[5];
};
int main()
{
Student student1;
student1.name = "Jack" ;
student1.grades[0] = 85;
student1.grades[1] = 92;
student1.grades[2] = 76;
student1.grades[3] = 81;
student1.grades[4] = 90;
cout << "Student Name: " << student1.name << endl;
cout << "Grades: " ;
for ( int grade : student1.grades) {
cout << grade << " " ;
}
cout << endl;
return 0;
}
|
Output
Student Name: Jack
Grades: 85 92 76 81 90
Difference Between Array of Structures and Array Within a Structure in C++
The below table demonstrates the key differences between an array of structures and an array within structures:
Feature
|
Array of Structures
|
Array of Structures
|
Definition
|
It is declared as an array where each element is a structure.
|
A structure contains an array as one of its members.
|
Syntax
|
struct Point { int x; int y; };
Point points[3];
|
struct Person { char name[50];int grades[5]; };
Person person1;
|
Memory Allocation
|
Memory is allocated for every structure element separately.
|
Memory is allocated for the entire structure, including the array within it.
|
Memory Efficiency
|
It can be a memory-saver if structures have a fixed size and minimal unused space.
|
It may have higher memory overhead, especially if arrays within structures vary in size.
|
Code Readability
|
Enhances code readability by providing a clear structure for related data.
|
Improves code readability by encapsulating arrays within the context of a single structure.
|
Accessing Data
|
Enhances code readability by providing a clear structure for related data.
|
Accessing data involves indexing the structure and then the array within it.
|
Conclusion
In conclusion, if you’re dealing with many items that are alike and you want to work with them individually, go for an array of structures. But if you need to store multiple details about a single item, having an array within a structure will serve you better. Both ways help make your code more readable and organized, saving you time and effort when working with complex data.
Share your thoughts in the comments
Please Login to comment...