C++ accessing an array

In the previous post we have discussed how to declare and initialized an array, in this post let us see how to access an array and manipulate them.

Before we begin just a small reminder, the number of initializers for any array is same as the number of the index value, except for the C type string where the index value is one more than the number of characters in the initializer string. Also note, the initializer of an array always lie in a sequence meaning they lie one after another in a memory.

Link Array declaration and initialization

To access the array element we will use the array syntax, that means we will attach the subscript ‘[]’ after the array name. To access a specific value, we will mention the index of that value.

The index value will depend on the position of the value where it is written in the sequence. Consider the array ‘arr’ below.

int arr[4]={ 303 , 404 , 405 , 505 } ;

If you want to access the value ‘404’ from the array, we will use the expression arr[1].

In an array, the index value count begins from 0, so the index value corresponding to 404 is 1. Just think of arr[4] as a combination of arr[0], arr[1], arr[2] and arr[3] and the initializer 303 is assign to arr[0], 404 to arr[1], 405 to arr[2] and 505 to arr[3]. Look at the code below where we will output each of the array value.

cout<< arr[1] << endl  //output 404
  << arr[0] << endl  //output 303
   << arr[2] ;  //output 405

More example is given below.

int ia[4]={ 121 , 3 , 80 } ;

cout<< ia[2] << endl //prints 80
 << ia[3] << endl; //prints 0

double darr[]={ 3.45 , 6.78 , 0.958 , 3 } ;

cout<< darr[3] << endl //prints 3
 << darr[1] << endl; //prints 6.78

string sarr[]={ “Happy” ,
   “Sad” ,
   “Lively ” };

cout<< sarr[2] << endl //prints “Lively”
   << sarr[0] << endl; //prints “happy”

Why we can access an array using a consecutive index value 0,1,2,3,4,..

Accessing the array element using the consecutive index value 0,1,2 … and so on is possible because the elements in an array are stored in a consecutive manner. The element lies next to each other in a storage.


Accessing an array using ‘auto’ (C++11 features)

There is another way to access the array element using for( ) statement, this method is known as range-based for loop and it is introduced in C++11. The method is shown below.

Link : C++11 range-based for loop

char carr[]={ ‘Y’ , ‘e’ , ‘s’ , ‘/’ , ‘N’ , ‘o’ } ;

for (auto ele:carr )
cout<< ele ;

The output is still,


Assigning an array to another array

C and C++ does not allow an array to be assigned to another array. In other words, it does not allow copying of array. Consider the code below.

int i[]={ 2,345 ,47} ;

int ia[]=i ; //error!

ia=i ;//error!

If you want to copy the values of one array to another then you can use the for() or while() statement to copy the value one by one.

for(int i=0; i<3;i++ ) { ia[i]=i[i] ; } 

Side note

An interesting case I have encountered while trying to access an array is that exchanging an array name with its index value in the array expression will still yield the same value. Consider the code below.

int ia[]={2,3,4,5};

cout<< ia[1] << endl 
<< 1[ia] ;

The output is,


As you can see ‘1[ia]’ gives the same output as ‘ia[1]’ although the index and array name are exchanged in the expression ‘1[ia]‘.

More about this is discussed here C++ exchanging the array name and index value when accessing the array element