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.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

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
include_once 'codzify-footer.php'; ?>