A structure is a data type in C/C++ that allows a group of related variables to be treated as a single unit instead of separate entities. A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member. Such an array is called an array within a structure. An array within a structure is a member of the structure and can be accessed just as we access other elements of the structure.
Below is the demonstration of a program that uses the concept of the array within a structure. The program displays the record of a student comprising the roll number, grade, and marks secured in various subjects. The marks in various subjects have been stored under an array called marks. The whole record is stored under a structure called a candidate.
Roll number : 1 Grade : A Marks secured: Subject 1 : 98.50 Subject 2 : 77.00 Subject 3 : 89.00 Subject 4 : 78.50
An array is a collection of data items of the same type. Each element of the array can be int, char, float, double, or even a structure. We have seen that a structure allows elements of different data types to be grouped together under a single name. This structure can then be thought of as a new data type in itself. So, an array can comprise elements of this new data type. An array of structures finds its applications in grouping the records together and provides for fast accessing.
Below is the demonstration of an array of structures. The array holds the details of the students in a class. The details include the roll number, grade, and marks, which have been grouped under a structure (record). There exists one record for each student. This is how a collection of related variables can be assembled under a single entity for enhancing the clarity of code and increasing its efficiency.
Roll number : 1 Grade : A Average marks : 89.50 Roll number : 2 Grade : C Average marks : 67.50 Roll number : 3 Grade : B Average marks : 70.50
Below is the tabular difference between the Array within a Structure and Array of Structures:
|Array within a Structure||Array of Structures|
|Basic idea||A structure contains an array as its member variable||An array in which each element is of type structure|
|Access||Can be accessed using the dot operator just as we access other elements of the structure||Can be accessed by indexing just as we access an array|
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between C structures and C++ structures
- Maximum number of threads that can be created within a process in C
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- How to call function within function in C or C++
- Structures in C
- Policy based data structures in g++
- C program to store Student records as Structures and Sort them by Name
- C program to store Student records as Structures and Sort them by Age or ID
- Structures in C++
- Slack Bytes in Structures : Explained with Example
- C/C++ program to add N distances given in inch-feet system using Structures
- What are the C programming concepts used as Data Structures
- Flexible Array Members in a structure in C
- Difference between Structure and Array in C
- Structure Member Alignment, Padding and Data Packing
- Structure vs class in C++
- Anonymous Union and Structure in C
- Structure Sorting (By Multiple Rules) in C++
- C | Loops & Control Structure | Question 1
- C | Loops & Control Structure | Question 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.