While I think Codor's solution is in best compliance with the coding style of C++, I would like to share my solution done in C-style.
#include <iostream>
typedef int (*pint2)[2]; //pint2 is a pointer to an array of 2 ints
int main()
{
int arr[4] = { 0, 1, 2, 3 };
pint2 parr = (pint2)arr;
std::cout << parr[0][0] << std::endl;
std::cout << parr[0][1] << std::endl;
std::cout << parr[1][0] << std::endl;
std::cout << parr[1][1] << std::endl;
getchar();
return 0;
}
Hope this helps! (or at least that you found this interesting :/)
EDIT: And for arrays of variable lengths!
#include <iostream>
int main()
{
int arr[12] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
const int sizeofarr = sizeof(arr)/sizeof(arr[0]); //Number of elements in array
{
const int numofrows = 2; //Number of rows
const int numofcolumns = sizeofarr/numofrows; //Number of columns
typedef int (*pinta)[sizeofarr/numofrows]; //A 2D array of columns of 2
pinta parr = (pinta)arr;
for(int i = 0; i < numofrows; ++i)
for(int j = 0; j < numofcolumns; ++j)
std::cout << parr[i][j] << std::endl;
}
{
const int numofrows = 3; //Number of rows
const int numofcolumns = sizeofarr/numofrows; //Number of columns
typedef int (*pinta)[sizeofarr/numofrows]; //A 2D array of columns of 3
pinta parr = (pinta)arr;
for(int i = 0; i < numofrows; ++i)
for(int j = 0; j < numofcolumns; ++j)
std::cout << parr[i][j] << std::endl;
}
getchar();
return 0;
}