Initializing the Elements of a Multidimensional Array

As with any array, we can initialize the elements of a multidimensional array by providing a bracketed list of initializers. Multidimensional arrays may be initialized by specifying bracketed values for each row:

int ia[3][4] = {    // three elements; each element is an array of size 4    {0, 1, 2, 3},   // initializers for the row indexed by 0    {4, 5, 6, 7},   // initializers for the row indexed by 1    {8, 9, 10, 11}  // initializers for the row indexed by 2};

The nested braces are optional. The following initialization is equivalent, although considerably less clear:

// equivalent initialization without the optional nested braces for each rowint ia[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; ...

Get C++ Primer, Fifth Edition 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.