Preparing Array of Structures – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#2938 siteicon   siteicon   siteicon  

Preparing Array of Structures

Program to demonstrate how an array of structures is prepared to get entry of multiple similar type of data entries.

Learning Objectives

  • Preparing array of structures so that multiple instances of similar data records can be stored.

Source Code

TC++ #2938


Source Code

Run Output

Enter Student Name : Ram Kumar
Enter Marks out of 100: 77
Enter Student Name : Amar Nath
Enter Marks out of 100: 81
Enter Student Name : Vijay Das
Enter Marks out of 100: 82

Entered data is --
Ram Kumar, 77
Amar Nath, 81
Vijay Das, 82

Code Understanding

struct Student { char name[60]; int marks;};
This structure can collect data of name and marks of Student.

Student s[3];
This is array of structures. Here for convenience sake we have taken just a size of 3. We can make as many students we like for such an array. We have created this array before main() so that even external functions out of main can also use it if desired. This is a global declaration.

for(int i=0;i<3;i++) //This loop is for filling structure array
cout<<“Enter Student Name : “; cin.getline(s[i].name,60);
Here we collect the name. For each instance of array we use the iteration count i and write as s[i].name. Here i begins from 0 so the array is correctly filled from index 0.

cout<<“Enter Marks out of 100: “; cin>>s[i].marks;
Similarly marks are also collected.

We use this as in second iteration onwards the newline character of marks entry has to be ignored. This is a requirement of cin.getline(). read more about this in code notes of #2781.
cout<<endl<<“Entered data is — “<<endl; //This is message before printing the data

for(int i=0;i<3;i++) { cout<<s[i].name<<“, “<<s[i].marks<<endl; }
Using this loop the data filled in previous loop is printed. Here again array index count is used.



  • Array of structures is a good and simple technique to create database records type of entry. However the limitation of number of records is evident as the array size has to be fixed.

Common Errors

  • Not using cin.ignore is the common error students do. Students may use gets() function of C, but it is generally not recommended as it can take unlimited string data and corrupt memory of nearby variables.

Suggested Filename(s): struarr.cpp


sunmitra| Created: 22-Jan-2018 | Updated: 19-Dec-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues