• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Pointer » How to Declaring Array of Structures in c++
Next →
← Prev

How to Declaring Array of Structures in c++

By Dinesh Thakur

An array of structures refers to an array in which each element is of structure type. To declare an array of structures, firstly, a structure is defined and then an array of that structure is declared. The syntax for declaring an array of structures is

structure_name structure_variable[size];

To understand the concept of declaring an array of structures, consider this example.

Example : A code segment to declare an array of structures

struct sales_record {
  //same as
} ;
sales_record item[2]; //an array of structures

In this example, an array item of type structure sales_record is declared. That is, the array elements item [0] and item [1] are structure variables of structure type sales_record. In addition, the compiler allocates 18 bytes of memory space (9 bytes for each element) to item and the array elements item [0]and item [1]are stored in adjacent memory locations.

Now, the array elements can be initialized using this statement.

sales_record item[]={ {'A',151,423,86.53}, {'C',152,654,90.38} };

In this statement, the elements of array item are initialized with specific values and the size of array is automatically set to two.

Once an array of structures is declared and initialized, the members of the array (that is, structure variables) can be accessed using the dot operator. The syntax for accessing members of an array of structures is

structure_variable[index].member_name

where, index can be any integer variable or integer constant or an expression evaluated to integer value.

For example, the array elements of item can be accessed using these statements.

cout<<item[0].grade;     //display grade of 1st array element
cout<<item[1].item_rate; //display item_rate of IInd 
                         //array element

To understand the concept of array of structures, consider this example.

Example: A program to illustrate the usage of array of structures

#include<iostream>
using namespace std;
const int size=2; //size of array
struct employee //structure definition {
   int emp_no;
   char name [10];
   float salary;
} ;
int main () {
   //array within structure
   employee emp[size]; //declaring array of structures emp
   for(int i=0;i<size;i++) //reading data into emp
   cout<<"Enter' details for employee #"<< (i+l)<<": \n";
   cout<<" Code: ";
   cin>>emp[i].emp_no; //accessing members
   cout<<" Name:”;
   cin>>emp[i]name;
   cout<<" Salary:";
   cin>>emp[i] .salary;
   cout<<"\n" ;
   for(int i=0;i<size;i++) //displaying data of emp {
     cout<<"Details of employee #"<<(i+1)<<" are:\n";
     cout<<" Code: "<<emp[i] .emp_no<<"\n" ;
     cout<<" Name: "<<emp[i] .name<<"\n";
     cout<<" Salary: "<<emp[i] .salary<<"\n";
}
  return 0;
}

The output of the program is

Enter details for employee #1:

Code: 101

Name: Smith

Salary: 9545.50

Enter details for employee #2:

Code: 102

Name: Robert

Salary: 8567.35

Details of employee #1 are:

Code: 101

Name: Smith

Salary: 9545.5

Details of employee #2 are:

Code: 102

Name: Robert

Salary: 8567.35

In this example, an array emp of structure type employee is declared. The values of members of emp are accepted from the user and are then displayed.

You’ll also like:

  1. How to Declaring Nested Structures in c++
  2. Write a C++ Program of Array of Structures
  3. How to Declaring and Accessing Structure Variables in c++
  4. difference between declaring a variable in the General Declaration section of a standard code module and declaring variable in the general declaration section of a form code module?
  5. Declaring and Allocating Arrays in Java
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW