Greetings. I am working on an exercise where I have to print a two dimensional array using pointers. I can't use any of auto, decltype, or type aliases. The code below is the sum total of my efforts since yesterday. I think my outer loop is correct, but I am stuck there I can't go any further. If someone could help me out or point me in the right direction I would be grateful.
p.s. This isn't homework it's a hobby, so feel free to spell it out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
int a[3][4] = { {0,1,2,3},
{4,5,6,7},
{8,9,10,11} };
for( int (*p)[4] = a; p < a + 3; p++ )
{
}
return 0;
}
I took another stab at it, and came up with the following, which actually works. Now all I have to do is understand what I did! I'd still like to talk it over with someone "in the know".
We could make a game out of this. How many ways can this be done. I was thinking mostly of using pointers in the loop control lines, but I like your method too.
#include <iostream>
#include <iomanip>
int main()
{
int a[3][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} };
using row = int[4] ; // type of a row of the two-dimensional array 'a' is 'array of 4 int'
const row* begin = a ; // 'pointer to const row' ie. 'pointer to const array of 4 int'
const row* end = a+3 ;
for( const row* p = begin ; p < end ; ++p )
{
// type of 'p' is 'pointer to const row' ie. 'pointer to const array of 4 int'
// type of '*p' is 'const row' ie. 'const array of 4 int'
constint* row_begin = *p ;
constint* row_end = row_begin + 4 ;
for( constint* q = row_begin ; q < row_end ; ++q ) std::cout << std::setw(3) << *q ;
std::cout << "\n\n" ;
}
}