In this C++ tutorial, you shall learn about array of arrays, how to create an array of arrays of specific size, and how to traverse the inner arrays using For loop, with examples.

C++ Array of Arrays

C++ Array of Arrays is an array in which each element is an array of elements.

To declare an array of arrays use the following syntax.

</>
Copy
datatype array_name[size1][size2]

where

  • datatype is the type of elements stored in the inner arrays.
  • array_name is the name given to this array, using which we reference the array in later part of the program.
  • size1 is the number of inner arrays.
  • size2 is the number of elements in each of the inner array.

Examples

1. An array of integer arrays

In the following example, we create an array of integer arrays. We traverse the array of arrays using For Loop and print them to console.

main.cpp

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int x[2][3] = { {2, 4, 6}, {10, 20, 30} };
    for (int i = 0; i < 2; i++) {
        for (int k = 0; k < 3; k++) {
            cout << x[i][k] << "\t";
        }
        cout << endl;
    }
}

Output

2	4	6	
10	20	30	
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned how to declare/initialize an Array of Arrays, and how to traverse through the elements of this nested array, with the help of example C++ programs.