DEV Community

Jess
Jess

Posted on

C++ Arrays

Arrays allow you to create a single variable to store multiple values in contiguous memory space. In C++ you have to declare the data type the array will hold and you cannot mix data types in a single array.

Declaration, Initialization, and Manipulation

int numbers[10]; declares an array called numbers that can hold 10 integers.

Array indices begin at 0, so your first value will be at numbers[0] and your last value will be at numbers[9].

To initialize an array, you list values between curly brackets:

int numbers[] = { 9, 3, 4, 0, 1 };

When you initialize during declaration you don't need to specify the size of the array. If you want a larger array and only want to initialize some of the values you can include the size:

int numbers[10] = { 9, 3, 4, 0, 1 }; will initialize numbers[0] through numbers[4] with the specified values and the rest will be initialized to 0.

int numbers[10] = { 9, 3, 4, 0, 1 };

for (int i = 0; i < 10; i++)
  std::cout << i << ": " << numbers[i] << std::endl;

// output
0: 9
1: 3
2: 4
3: 0
4: 1
5: 0
6: 0
7: 0
8: 0
9: 0
Enter fullscreen mode Exit fullscreen mode

You can assign values to the remaining indices:

numbers[5] = 8;
numbers[6] = 2;
Enter fullscreen mode Exit fullscreen mode

You can also change values previously set:

numbers[0] = 47;

// output
0: 47
1: 3
2: 4
3: 0
4: 1
5: 8
6: 2
7: 0
8: 0
9: 0
Enter fullscreen mode Exit fullscreen mode

Out of Bounds

If you try to set a value to an index that is negative or beyond the size of the array - 1 (in this example, numbers[10]) it will be out of bounds. C++ does not check if the index is in range, so you might end up accessing and altering data you didn't intend to. You might also access protected memory which will halt your program. Some compilers may terminate with an error message if an array index goes out of bounds.

Passing Arrays as Parameters to a Function

An array is passed to functions by reference, so if you change the values within the function, it changes the actual values in the array.

void doubleValues(int arr[], int size)
{
  for (int i = 0; i < size; i++)
    arr[i] = arr[i] * 2;
}

void printValues(int arr[], int size)
{
  for (int i = 0; i < size; i++)
    std::cout << i << ": " << arr[i] << std::endl;

  std::cout << std::endl;
}

int main() {
  int numbers[] = { 1, 2, 3, 4, 5 };


  printValues(numbers, 5);
  doubleValues(numbers, 5);
  printValues(numbers, 5);

  return 0;
}

// output
0: 1
1: 2
2: 3
3: 4
4: 5

0: 2
1: 4
2: 6
3: 8
4: 10
Enter fullscreen mode Exit fullscreen mode

You can prevent a function from changing the values of an array parameter by using the reserved word const. You should always declare an array to be a constant formal parameter if you don't intend for the function to modify it.

void doubleValues(const int arr[], int size)
{
  // ...
}
Enter fullscreen mode Exit fullscreen mode

Multidimensional Arrays

A two-dimensional (2D) array contains data arranged in rows and columns.

int grid[5][4]; declares a 2D integer array with 5 rows and 4 columns.

    0     1     2     3
0 [ 0 ] [ 0 ] [ 0 ] [ 0 ]
1 [ 0 ] [ 0 ] [ 0 ] [ 0 ]
2 [ 0 ] [ 0 ] [ 0 ] [ 0 ]
3 [ 0 ] [ 0 ] [ 0 ] [ 0 ]
4 [ 0 ] [ 0 ] [ 0 ] [ 0 ]
Enter fullscreen mode Exit fullscreen mode

To access components of a 2D array you use the index of the row followed by the index of the column of the value you want to access.

grid[2][1] = 9;

for (int i = 0; i < 5; i++)
{
  std::cout << i << " ";

  for(int j = 0; j < 4; j++)
    std::cout << "[ " << grid[i][j] << " ]" << " ";

  std::cout << "\n";
}

// output
0 [ 0 ] [ 0 ] [ 0 ] [ 0 ]
1 [ 0 ] [ 0 ] [ 0 ] [ 0 ]
2 [ 0 ] [ 9 ] [ 0 ] [ 0 ]
3 [ 0 ] [ 0 ] [ 0 ] [ 0 ]
4 [ 0 ] [ 0 ] [ 0 ] [ 0 ]
Enter fullscreen mode Exit fullscreen mode

In the above example, you can also see that you can iterate over the rows and columns using nested loops. Here i represents the rows and j represents the columns.

2D arrays can also be initialized during declaration, similar to how one dimensional arrays are. Each index of the outer curly braces represents a column and it contains a set of values inside curly braces that represent the row.

int grid[3][3] = {
  {1, 2, 3},
  {4, 5, 6},
  {7, 8 , 9}
};

  std::cout << "    0     1     2";
  std::cout << std::endl;

  for (int i = 0; i < 3; i++)
  {
    std::cout << i << " ";

    for(int j = 0; j < 3; j++)
      std::cout << "[ " << grid[i][j] << " ]" << " ";

    std::cout << "\n";
  }
Enter fullscreen mode Exit fullscreen mode

Further Reading / References

  • C++ Programming: Program Design Including Data Structures 8th ed. by D.S. Malik

Top comments (0)