position: fixed; top: auto !important; margin-left: 112px;

Arrays in C++

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 <iostream>
using namespace std;

int main() {
 int age[5];
   for (int i = 0 ; i <= 5 ; i++ )
    {
     cout <<"\nEnter age of Student " ;
     cin >> age[i];
     }
    
     for (int i = 0 ; i <= 5 ; i++ )
    {
     cout <<"\nDisplay 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 <iostream>
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 <iostream>
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<<arr<<"\n";
     }

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 <iostream>
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 <<"\n";
}

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.

Solve the Quiz of Article

1) If variable is declared as float, what do you think compiler will allocate memory of 8 bytes?
Yes
No

2) Does void return some value?
Yes
No


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.