Multidimensional arrays in C++ are arrays of arrays. A two-dimensional array is really a one-dimensional block of memory with a fixed row width, and understanding that is what makes the rest fall into place. It is the indexing, the passing to functions, and the surprising difference between a row pointer and a row array.
The previous lesson in this module covered "Which std::string Methods Handle Everyday Text?", and this lesson is the last one in the Arrays and Strings module.
Declaring a grid
A two-dimensional array declares a number of rows and a number of columns. Both have to be constant expressions.
#include <iostream>
int main() {
int shelf[3][4]; // three rows of four ints each
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 4; ++col) {
shelf[row][col] = row * 10 + col;
}
}
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 4; ++col) {
std::cout << shelf[row][col] << ' ';
}
std::cout << '\n';
}
return 0;
}The output is the grid, filled row by row.
0 1 2 3
10 11 12 13
20 21 22 23How the memory is laid out
It is twelve consecutive int values, and the compiler works out the address of shelf[row][col] by multiplying out the offsets.
#include <iostream>
int main() {
int shelf[3][4];
std::cout << "sizeof(shelf): " << sizeof(shelf) << '\n';
std::cout << "sizeof(shelf[0]): " << sizeof(shelf[0]) << '\n';
std::cout << "sizeof(shelf[0][0]): " << sizeof(shelf[0][0]) << '\n';
std::cout << "address of [0][0]: " << &shelf[0][0] << '\n';
std::cout << "address of [1][0]: " << &shelf[1][0] << '\n';
return 0;
}The addresses of [0][0] and [1][0] differ by exactly one row, which is the proof that the rows are laid out one after another.
sizeof(shelf): 48
sizeof(shelf[0]): 16
sizeof(shelf[0][0]): 4
address of [0][0]: 0x7ffef53cc720
address of [1][0]: 0x7ffef53cc730Passing a grid to a function
The column count survives, because it is needed to work out the offset of an element. The row count does not survive, so it has to be passed separately.
#include <iostream>
void print_grid(const int grid[][4], int rows) {
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < 4; ++col) {
std::cout << grid[row][col] << ' ';
}
std::cout << '\n';
}
}
int main() {
int shelf[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
print_grid(shelf, 2);
return 0;
}The 4 has to be there, because without it the compiler cannot work out how to find grid[row][col].
1 2 3 4
5 6 7 8The row pointer trap
A pointer to a row and an array of rows are different types, and their sizes differ.
#include <iostream>
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*row_pointer)[3] = grid; // pointer to the first row
std::cout << "sizeof(grid): " << sizeof(grid) << '\n';
std::cout << "sizeof(row_pointer): " << sizeof(row_pointer) << '\n';
std::cout << "grid[1][2]: " << grid[1][2] << '\n';
std::cout << "pointer[1][2]: " << row_pointer[1][2] << '\n';
return 0;
}The output shows that the grid is 24 bytes while the pointer is only 8.
sizeof(grid): 24
sizeof(row_pointer): 8
grid[1][2]: 6
pointer[1][2]: 6Three dimensions and beyond
A three-dimensional array is an array of arrays of arrays, and it decays into a pointer to an array of arrays.
#include <iostream>
int main() {
int cube[2][2][2];
int value = 1;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
cube[i][j][k] = value++;
}
}
}
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
std::cout << cube[i][j][k] << ' ';
}
std::cout << '|';
}
std::cout << '\n';
}
return 0;
}The output shows the eight values in the order that they were stored.
1 2 |3 4 |
5 6 |7 8 |Filling a grid and summarising it
Here is a small sales table, and the column totals that are worked out from it.
#include <iostream>
int main() {
const int rows = 3;
const int cols = 3;
int sales[rows][cols] = {
{12, 18, 9},
{22, 15, 30},
{8, 11, 27}
};
int column_totals[cols] = {0, 0, 0};
int grand_total = 0;
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
column_totals[c] += sales[r][c];
grand_total += sales[r][c];
}
}
for (int c = 0; c < cols; ++c) {
std::cout << "column " << c << " total: " << column_totals[c] << '\n';
}
std::cout << "grand total: " << grand_total << '\n';
return 0;
}The output shows the totals.
column 0 total: 42
column 1 total: 44
column 2 total: 66
grand total: 152When a vector of vectors is better
A std::vector of std::vector trades a little performance for flexibility.
#include <iostream>
#include <vector>
int main() {
std::vector<std::vector<int>> shelf;
for (int row = 0; row < 3; ++row) {
std::vector<int> books;
for (int col = 0; col < 4; ++col) {
books.push_back(row * 10 + col);
}
shelf.push_back(books);
}
for (const auto& row : shelf) {
for (int value : row) {
std::cout << value << ' ';
}
std::cout << '\n';
}
return 0;
}The output is identical to the raw array version, and the rows can now be different lengths and they can grow at run time.
0 1 2 3
10 11 12 13
20 21 22 23Quick reference
- A two-dimensional array is a block of memory with a fixed row width.
- The size of a row is the column count multiplied by the size of one element.
- The column count must appear in a function parameter, and the row count has to be passed separately.
- A pointer to a row and an array of rows are different types, and the pointer is much smaller.
- Three dimensions and beyond work in exactly the same way.
- Use a vector of vectors when the rows have to change size or have different lengths.
Try it yourself: write a program that fills a three by four grid with the numbers 1 to 12, and then works out the total of each row and the total of each column. Print both. Then change the grid to a vector of vectors where one row has six entries instead of four, and confirm that the row totals still come out right.