Multi-dimensional arrays and pointers.

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>

using namespace 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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace 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++ )
    {
        for( int *q = *p; q != *p + 4; q++ )
        {
            cout << *q << " ";
        }
        cout << "\n";
    }

    return 0;
}


edit: *p must be a pointer so I must be dereferencing a pointer to a pointer. I think.
Last edited on
Is this considered cheating? Haha

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

int main()
{
	int a[3][4] = { { 0,1,2,3 },
			{ 4,5,6,7 },
			{ 8,9,10,11 } };

	int *p = &a[0][0];

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			*p = *(*(a + i) + j);
			cout << *p << " ";
		}
		cout << endl;
	}

	std::cin.get();
	return 0;
}
Last edited on
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.

Boy this stuff really makes me go bug-eyed.
Pointers always confuse me. This points to this which points to that. By the end I forget what the first one pointed to and I'm screwed. :D
> Pointers always confuse me. This points to this which points to that.
> By the end I forget what the first one pointed to

Using type aliases would help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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'
        const int* row_begin = *p ;
        const int* row_end = row_begin + 4 ;

        for( const int* q = row_begin ; q < row_end ; ++q ) std::cout << std::setw(3) << *q ;
        std::cout << "\n\n" ;
    }
}

http://coliru.stacked-crooked.com/a/9da66deeaed6ea73
Last edited on
Topic archived. No new replies allowed.