C++ array : myths and conception

With variable we are allowed to manipulate a single data or value.Say a variable of int type can store only one single integer value.What if we want to store many integer value or in our program a case may arise when we requires lot of variables or a sequence of values of specific type.In such case it is better to make use of array instead or declaring lots of variable.In this post we will see what is an array? and some of the ways to initialized them.

what is an array?

By array we simply mean a collection of data of particular type store in a sequential manner and by collection we mean holding many values.Note an array is also a variable except it can hold many values.

Array is declare in the same way as the normal variables are declared.But to distinguish an array from the normal variables we will add the opening and closing square bracket ‘[]’ after the variable name.The number of values the array should hold is mentioned inside the square bracket;this value is known as index value.So an ‘index’ of array determine the number of data the array can hold.An example of array declaration is shown below.

int ivalue; //normal variable

int iarr[2] ; //an array declaration

Note ‘ivalue’ is a normal variable and ‘iarr[2]’ is an array.The ‘2’(index value) inside ‘[]’ in ‘iarr’ shows that ‘iarr’ can hold 2 value or data.In other words ‘iarr’ is worth two variables.

But if we had written ‘iarr’ as iarr[5] then note it can hold five integers value due to index value being 5.So the number of data an array can hold depend on the value mentioned inside the square bracket.

The maximum value an ‘index’ can take is limitless as long as there is enough memory available.

Initializing an array

We have seen that declaring an array is pretty simple.And before we start using an array we have to initialize them.There are two ways to initialize an array.

i)Explicit initialization and
ii)Using loop-for or while- statement.

The second method will be discussed in another post.

Explicit initialization

In an explicit initialization method of array when an array is declared,we assign the array with the number of value(also known as initializer) equal to the index value inside a braces(‘{}’).Consider the code below.

Link :Initializer

int iarr[5]={ 2 , 4 , 5 , 5 , 67 } ; //integer array initialization

char carr[3]={‘G’ , ‘O’ , ‘D’ } ; //char array initialization

string sarr[2] ={ “New shoes”
  “New games” }; //string array initialization

double darr[4]={ 3.45 , 4e10 , 5.6 , .0098 } ;   ///double array initialization

for iarr[5] the index value is 5 so 5 integers inside ‘{}’ is assign to it.Similarly,for carr[3] three characters inside ‘{}’ is assign to it and so is for sarr[2] with two text and darr[4] with 4 real number inside ‘{}’.

In the above program the number of initializer inside the braces is equal to the index of the array so a question may arise,what if the number of initializers provided is less than or greater then the index value? In such case we will get varying outcome depending on whether the initializer provided is less or more.Consider the program below.

/** if the initializers provided is less then the array index value ***/

int iarr[5]={2,3,4 } ; //two more initializer required

char carr[3]={‘G’ , ‘O’ } ; //the 3rd array is assigned ” (empty character)

string sarr[4] ={ “New shoes”
    “New games”}; //the 3rd and 4th array is assigned “” (empty string)

double darr[4]={ 3.45 , 4e10 , 5.6 } ; //the 3rd array is assigned 0

So note if the initializer provided is less than the index value then:

For ‘int’ type array iarr[5] only three initializers are given so two more is required,the two more initializers is treated as 0.So now,{2,3,4} is same as {2,3,4,0,0}.

For ‘char’ type array carr[3],the one remaining initializer is treated as an empty character written as ”.This empty character is not same as ‘ ‘.The ‘ ‘ has space as it’s value whereas ” has no value.

For ‘string’ type array sarr[4] the two reamainign string is an empty string written as “”.

For ‘double’ type array darr[4] the one remaining initializer is treated as 0 like the int type.


For the other case when the number of initializers passed is more than the index value it is simply an error,the program cannot be executed.

/*** if the elements is more then the array index value ***/

int ir[3]={ 2,3,45,5} ; //error! ‘too many initializers’

string sr[2]={“New era” , ” of ” , “C++!” } ; //error! ‘too many initializers’

In C string,index value should be 1 greater than the characters in the string initializer

If you are coming from C or have a back ground in C you will often see a string usually written as,

char str[4]="low" ; //work fine

The above way of initializing a ‘char’ type array also work fine in C++,but the important thing to note when using such method is the number of characters provided inside the “”(double quotation) should be 1 less then the index value.If not you get an error.Consider the case below.

char str[3]="low" ; //error,'str' index is 3 the text "low" has 3 characters

Why is the above way to initialization an error.The reason for this is mainly due to C way of treating a string differently.According to C each string is appended with ‘\0’ character at the end of the string automatically.So the above “low” string is actually interpreted as “low\0”,which is a 4 characters string.But ‘str’ has only 3 index and according to the rule when the initializers provided is more than the index value we get an error,hence the error.

C++ has a different way of treating a string,we will discuss that in Chapter 7.


How to solve the string ‘char’ array index problem

According to the rule the number of index value must be one more than the number of characters in a string in a char type array.However,sometime we cannot predetermine how many characters a string may have so determining the index value is also impossible.To solve this problem another way of initializing a char array is introduced.To make the array variable more robust in this method we will not be mentioning the index value.

int ar[]={ 4,4,5,6} ;

string star[]={ “C++” , “C++11” , “C++14” , “C++17 coming soon” };

char car[]=”C type array string” ; //work fine

Here we have not provided the index value so we are letting the compiler to deduce the index value.

The advantage with this method is since the compiler has to figure out the index value it cannot complain that “the initializers provided is too many”,it will figure out the right number by itself.


The disadvantage with this method is we do not know the index value before hand if the necessity arises.But this problem can be solved by performing simple operation:

sizeof(your_array)/sizeof(data_type),your_array is the array name and data_type is the type of the array.
which evaluate the total number of elements in an array.The sizeof() functions return the size of the whole array and to get the number of elements in array we divide it by the size of the array type.

int ia[]={ 5 , 5 , 6 , 73, 45 , 78 } ;

cout<< sizeof(ia)/sizeof(int) ; 

The output is,


Which is the number of initializers!


An array is indeed an efficient way to declare many variables also maintaining many values with array is also simpler we.In this post we have discussed how to declare and initialized an array in the next post we will see how to access each of the value in the array and manipulate it according to our needs.