Arrays in C++

Arrays are used to store a number of items of the same type in one single variable. They are usually placed in the same segment of memory with accessible elements for each. This tutorial will explain you the tricks to C++ arrays.

Array Declaration

To declare arrays in C++ you need to specify the type of the array elements, name of array variable and the number of elements required by an array.

type variableName [size];

Example

int foo[5];

Array Initialization

In C++ array elements can be initialized one by one or through a single statement.

int bar[12];
bar[0] = 8;
bar[3] = 2;

OR

int bar[12] = {8,1,3,2,9,7,4,0,5,11,12,6};

The number of values between braces {} can not exceed the size of array which was specified at the time of array declaration.

Accessing Array Elements

The value of any of the elements in an array can be accessed by indexing the array name. You can access the value of 5th element of bar[] array as follows.

count << bar[4];

Example

#include <iostream>
using namespace std;

int main () {

int bar[12] = {8,1,3,2,9,7,4,0,5,11,12,6}; // bar is an array of 12 integers

// output each element of array
for(int i = 0; i < 12; i++ ){

cout << bar[i] << “\n”;

}

return 1;

}

Multidimensional Arrays

Arrays of arrays are called multidimensional arrays. C++ allows multidimensional arrays.

Multidimensional array declaration

int foo[2][4];

Multidimensional Arrays Example

#include <iostream>
using namespace std;

int main () {

// an array with 5 rows and 2 columns.
int foo[5][2] = { {1,3}, {0,2}, {2,6}, {3,1},{9,1}};

// output each array element’s value
for ( int i = 0; i < 5; i++ ) {

for ( int j = 0; j < 2; j++ ) {

cout << “foo[” << i << “][” << j << “]: “;
cout << foo[i][j]<< endl;

}

}

return 0;

}

Output

foo[0][0]: 1
foo[0][1]: 3
foo[1][0]: 0
foo[1][1]: 2
foo[2][0]: 2
foo[2][1]: 6
foo[3][0]: 3
foo[3][1]: 1
foo[4][0]: 9
foo[4][1]: 1

Pointers in C

A pointer is a variable suitable for keeping memory addresses of other variables. The values you assign to a pointer are memory addresses of other variables or other pointers.

Pointers in C are characterized by their value and data-type. The value is the address of the memory location the pointer points to, the type determines how the pointer will be incremented/decremented in pointer or subscript arithmetic.

Pointers are used to manipulate arrays and they can be used to return more than one value from a function.

Pointers are declared by using the asterisk(*).
int *p;

Each variable has two attributes: address and value. The address is the location in memory. In that location, the value is stored. During the lifetime of the variable, the address is not changed but the value may change.

void main (void)
{

int i;
int * a;
i = 10;
a = &i;
printf (” The address of i is %8u \n”, a);
printf (” The value of i is %d\n”, i);
printf (” The value at that location is %d\n”, *a);

}

Output

The address of i is 631672
The value of i is 10
The value at that location is 10

Arrays and Pointers

An array is actually a pointer to the 0th element of the array. Dereferencing the array name will give the 0th element. This gives us a range of equivalent notations for array access. In the following examples, arr is an array.

Array
Pointer
arr[0] *arr
arr[1] *(arr+1)
arr[n] *(arr+n)

Arrays in C

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
Arrays in C is a data structure of multiple elements with the same data type. Array elements are accessed using subscript. The valid range of subscript is 0 to size -1.

Declaration of Array

int arr[10];

Example

void main(void)
{

int a[5];
int i;
for(i = 0;i<5;i++)
{

a[i]=i;

}

for(i = 0;i<5;i++)
{

printf(“%d value of I is = %d\n”,i,a[i]);

}

}

Output

1 value of I is = 0
2 value of I is = 1
3 value of I is = 2
4 value of I is = 3
5 value of I is = 4

Multidimensional Arrays

A multi-dimensional array of dimension n (i.e., an n-dimensional array or simply n-D array) is a collection of items which is accessed via n subscript expressions. Multidimensional arrays can be described as “arrays of arrays”.

Multidimensional Arrays Example

void main(void){

int a[3][2];
int i,j;

for(i = 0;i<3;i++){

for(j=0; j<2 ;j++) {

scanf(“%d”,&a[i][j]);

}

}

for(i = 0;i<3;i++){

for(j=0;j<2;j++) {

printf(“value in array %d\n”,a[i][j]);

}

}

}

Java Arrays

The array data structure is a collection of same type of variables. Java arrays are used to store fixed size sequential collection of same type of elements. Items in array are called elements and they can be accessed by their index. This is how to declare, initialize and access arrays in java.

//declaring array of type integer
int[] myArray;

//allocating memory
myArray= new int[10];

//assigning values
myArray[0] = 5;
myArray[1] = 10;
myArray[2] = 15;
myArray[3] = 20;
myArray[4] = 25;
myArray[5] = 30;
myArray[6] = 35;
myArray[7] = 40;
myArray[8] = 45;
myArray[9] = 50;

//retrieving values
System.out.println(“Value at index 0 = “+ myArray[0]);
System.out.println(“Value at index 5 = “+ myArray[5]);

Processing Arrays

Usually loops are used to process arrays. We can use for loop and foreach loop to process array.

Processing Array Example

public class ArrayExample {

public static void main(String[] args) {

int[] myArray= {5,10,15,20,25,30,35,40,45,50};

int total = 0;

// processing array elements

for (int i = 0; i < myArray.length; i++) {

System.out.println(“Value at index ” + i + ” is = ” + myArray[i]);

total = total + myArray[i];

}

System.out.println(“Total is = ” + total);

}

}

Output:

Value at index 0 is = 5
Value at index 1 is = 10
Value at index 2 is = 15
Value at index 3 is = 20
Value at index 4 is = 25
Value at index 5 is = 30
Value at index 6 is = 35
Value at index 7 is = 40
Value at index 8 is = 45
Value at index 9 is = 50
Total is = 275

foreach Loop

The foreach loop makes it more easy to process arrays in java. You don’t need to use the index to access array element.

foreach Example

public class ArrayExample {

public static void main(String[] args) {

int[] myArray= {5,10,15,20,25,30,35,40,45,50};

int total = 0;

// processing array elements

for (int myVal: myArray) {

System.out.println(myVal);

total = total + myVal;

}

System.out.println(“Total is = ” + total);

}

}

Output:

5
10
15
20
25
30
35
40
45
50
Total is = 275