Multidimensional arrays

Arrays can be multidimensional and to add another dimension you add another set of square brackets:

    int two[2];     int four_by_three[4][3];

The first example creates an array of two integers, the second creates a two-dimensional array with 12 integers arranged so that there are four rows of three columns. Of course, row and column are arbitrary and treat the two-dimensional array like a conventional spreadsheet table, but it helps to visualize how the data is arranged in memory.

Note that there are square brackets around every dimension. C++ is different to other languages in this respect, so a declaration of int x[10,10] will be reported as an error by the C++ compiler.

Initializing multidimensional arrays involves ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.