I want to write a loop, that can give me the dimensions of an array. I want to make it usable for any array and it should return the sizes of the dimensions.
int arr[3][5][10][9] ; cout << "dimension 1: " << sizeof(arr)/sizeof(arr[0]) << endl; cout << "dimension 2: " << sizeof(arr[0])/sizeof(arr[0][0]) << endl; cout << "dimension 3: " << sizeof(arr[0][0])/sizeof(arr[0][0][0]) << endl; cout << "dimension 4: " << sizeof(arr[0][0][0])/sizeof(arr[0][0][0][0]) << endl; cout << "dimension 5: " << sizeof(arr[0][0][0][0])/sizeof(arr[0][0][0][0][0]) << endl;
This should return 3,5,10,9
(and fail for the last statement).
So the pattern seems clear "each iteration add [0]
after arr
. The last iteration will fail, which should stop the while-loop.
How can I "concatenate + evaluate" the array name?
I would also appreciate help on what test checks "Will this fail?", or "Is there another dimension?" in C++, as I'm just learning it.