Arrays

Exploring the world of Arrays, that affect your programs more than you might think.

Introduction

Arrays are compound types which consist of a type specifier, an identifier, and a dimension.

  • C++ Primer, 4th edition

Let’s start with what this means.

Compound Types

A compund type is a type that is defined in terms of other types. Since this is the first time in this tutorial that we have come across compound types, I will explain why an array is a compound type.

An array is defined in terms of it’s elements. An array is essentially a collection of similar elements, so we can explain what an array is only if we know what the elements are.

How to Declare an array

All arrays are declared in the same way:

type_specifier identifier[number_of_elements];

This shows the declaration of array of dimension 1.

What is a Type Specifier?

A type specifier is pretty self explanatory, really. It just specifies the type of the array.

What is an identifier?

It is a way of uniquely identifying an array, which you would have explicitly stated while declaring them. Therefore, in int array[20], int is the type specifier, and array is the identifier.

What is the dimension?

You can think of an array as a matrix, really, when it’s actually just a collection of data next to each other in the RAM. So think of an array with one subscript (The [20] part in the example) to be the dimension. Becuase it only has one pair of square brackets, it has dimension one and you can think of it to be a column/row matrix. Therefore, an array with two subscripts (something like int array [20][10]) would be a two-dimensional matrix.

How to initialise them

In integer arrays (here, integer is the type specifier), the initialisation is pretty easy. There are two ways:

  • Declaring the array first (and then initialise it later) This is the same for all type secifiers:

Example:int array[20]; has space for 20 elements in it.

  • Declaring it and initialising the variables at the same time. Now, here are some important points.

For int arrays

What I’m giving as an example for int arrays can easily be used for any data type.

#include<iostream>
using namespace std;
int main()
{
/* The various ways of initializing while declaring are these*/
int arr[2] = { 1 , 5 }; //Gives an array with two elements 1 and 5
int a[2] { 1 , 2 }; //Also gives an array with two elements 1 and 2 
int a2[5] { 1 , 3 }; 
/*Gives an array with the first two elements initialized to 1 and 3 respectively,
and the other three are unitialised*/
int a3[] = { 1 , 3}; //Also possible, it just assumes the size of the array to be two
//some code 
return 0
}

For char Arrays (Read this if you know everything so far)

Character arrays can be initialised in the same ways, but becuause character arrays in C always used to end with a null pointer (/0), character arrays with string literals end in the same way.

A value, such as 42, in a program is known as a literal constant: literal because we can speak of it only in terms of its value; constant because its value cannot be changed. Every literal has an associated type. For example, 0 is an int and 3.14159 is a double.

  • C++ Primer

So, to initalise chracter arrays, we can do it in three ways, as shown in this program:

#include <iostream>
using namespace std;
int main()
{
char ca1[5] = { 'H' , 'e' , 'l' , 'l' , 'o' }; //Five characters, no problem
char ca2[5] = { "H" , "e" , "l" , "l" , "o" }; //Five strings, hmm...
char ca3[5] = { "H" , "e" , "l" , "l" , '\0'}; 
//Null pointer explicitly added. should work (it doesn't, the compiler will add it)
char ca4[] = "Hello"; //This will have six elements
// some code
return 0;
}