1. Introduction to C++: A Comprehensive Guide with Code Examples - 2023 2. Mastering Data Types in C++: A Comprehensive Guide with Codes and Examples (2023) 3. Learn About Variables and Types of Variables in C++ | Codzify.com 4. Control Statements in C++: A Comprehensive Guide for 2023 5. C++ Tutorial: Understanding Switch Statements with Codes and Examples in 2023 6. Understanding Memory Allocation and Pointers in C++: A Beginners Guide 7. Functions in C++ 8. Call by value and Call by Reference in C++ in depth 9. Array in C++ 10. 2d arrays in C++ 11. Classes and Objects in C++ 12. Static Functions in C++ 13. Constructors and Destructors in C++ - A Complete Guide with Examples 14. Mastering Copy Constructor in C++ - Shallow vs Deep Copy with Examples | Codzify 15. Understanding Friend Functions in C++ Made Simple! 16. Inline Functions in C++ 17. this Pointer in C++ 18. Mastering Inheritance in C++: Types and Examples Explained 19. Types of Inheritance in C++ 20. Polymorphism in C++ Explained with Codes and Examples in 2023 21. Templates in C++ 22. Getting the Value of a MultiMap in C++: Step-by-Step Guide with Examples 23. Multimap Find and Replace Operator in C++: Step-by-Step Guide - Codzify Topics 24. Exploring the Next_Permutation Algorithm without STL in C++ - Codzify Topics 25. C++ - The Difference Between Map and HashMap in STL 26. Updating Values in a std::multimap in C++ - Codzify Topics 27. Which data structure sorts the elements on insertion in C++ STL? 28. Can we implement Red Black Tree in c++ by STL containers? 29. How to Dynamically Declare an Array of Objects with a Constructor in C++ - A Step-by-Step Guide 30. What is the difference between a pointer and an object in C++? 31. Mastering Red-Black Trees with STLs Internal Implementation: A Step-by-Step Guide

Array in C++

Article by: Manish Methani

Last Updated: October 19, 2021 at 2:04pm IST
3 min 16 sec read

Suppose you want to store the name of 100 students in memory. How will you achieve this? There are two ways :

1) Create 100 variables and assign a name to individual variables like a = Manish, b = Nitesh, c = Ram etc........

2) Create a single variable and give it the size of 100 elements. This is called an Array.

C++ Array is used to store values in a memory of the same type at a time. Values of the same type mean either it stores values of int type or values of float type or char types at a time. You cannot store different types at a time in an array. int and float type data storage at the same time is not allowed when you initialise an array in C++.

Array Declaration

The basic syntax for array declaration in C++ is given below:-

type arrayName[numberOfelements];

Example:-

int student[5];

How to initialise an array?

Syntax to initialise an array in C++ is given below:-

Syntax : -

dataType arrayRefVar[] = {value0, value1, ..., valuek};
int a[] = {1,2,3,4,5};

While declaring an array you must provide the number of elements in square bracket else compiler will throw an error like this,

storage size of 'a' isn't known

While initializing an array, it is fine to provide the number of elements or you can also skip it.

Memory Structure

When we declare an array, memory structure looks like this

Address Value at that Address
1000 1
1004 2
1008 3
1012 4

First column shows address of variable and second column shows value at that variable.

Example:

In this example, array will be initialized by taking the input from user using for loop and then the elements will be displayed as follows:

#include 
using namespace std;

int main() {
 int age[5];
   for (int i = 0 ; i <= 5 ; i++ )
    {
     cout <<"
Enter age of Student " ;
     cin >> age[i];
     }
    
     for (int i = 0 ; i <= 5 ; i++ )
    {
     cout <<"
Display Student Info " ;
     cout << age[i];
     }
   return 0;
}

Output :

Takes input from user 6 times

Enter age of Student 1
Enter age of Student 2
Enter age of Student 3
Enter age of Student 4
Enter age of Student 5
Enter age of Student 6

Now Display the Info

Display Student Info 1
Display Student Info 2
Display Student Info 3
Display Student Info 4
Display Student Info 5
Display Student Info 6

Pass array to Functions using Call by Value

#include 
using namespace std;

// Function Prototype Declaration is compulsory. 
Otherwise you may see many errors
void displayStudentInfo(int arr);

// main() is where program execution begins.
#include 
using namespace std;

// Function Prototype Declaration is compulsory. 
void displayStudentInfo(int arr);

#include 
using namespace std;

int main() 
{
     // Initalise an array
     int a[] = {1,2,3,4,5};
     for (int i = 0 ; i < sizeof(a)/sizeof(a[0]) ; i++ )
     {
    
     displayStudentInfo(a[i]);
     }
     
    return 0;
}

  void displayStudentInfo(int arr)
     {
         cout<

Output :

1
2
3
4
5

In this program we initialised an array a and applied a loop to it. So that we can retreive information from that array one by one. To do that we passed value at 'i' index through displayStudentInfo function.

Pass array to Functions using Call by Reference

#include 
using namespace std;

// main() is where program execution begins.
void displayStudentInfo(int *arr);
int main() {
  int age[] = {1,2,3,4,5,6};
 
   for (int i = 0 ; i <= 5 ; i++ )
    {
      displayStudentInfo( &age[i] );
    }
   return 0;
}

  void  displayStudentInfo (int *age)
{
     cout << "Age = " << *age <<"
";
}

Output :

Age = 1
Age = 2
Age = 3
Age = 4
Age = 5
Age = 6
In this program we initialised an array age and
 applied a loop to it . So that we can retreive information 
from that array one by one. To do that we passed
 address at 'i' 
index through displayStudentInfo function.
 And catched it using pointers.

Learn Flutter, FlutterFlow,
Firebase & Angular.

Looking to learn how to create the production-ready apps
from scratch? Well, you are at the right place.

₹1299 or $15.56

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Explore Curriculum
₹1299 or $15.56

Flutter Mobile App Development Course

Explore Curriculum
₹1299 or $15.56

The Complete Angular Course

Explore Curriculum
₹1299 or $15.56

Dart Programming for Absolute Beginners

Coming Soon
FREE

Next.js course

Start Watching
FREE

Fundamentals of Computer Programming Languages

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer