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.

Discover My FlutterFlow Courses and Template Apps

Launch Your Dating App with FlutterFlow: Course + Template
Master FlutterFlow and Build Your Dating App with Our Step-by-Step Course and Ready-Made Template.
Launch Your Grocery Delivery App with FlutterFlow: Course + Template
Master FlutterFlow and Build Your Grocery Delivery App with Our Step-by-Step Course and Ready-Made Template.
Launch Your Courses App with FlutterFlow: Course + Template
Master FlutterFlow and Build Your Courses App with Our Step-by-Step Course and Ready-Made Template.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com