Printing 2D Arrays with Pointers

S

Super KHB

I know that I am close, but it wont work. HELP!!!!

void PrintA( int * , int);

void main()
{

int num[3][5] = {
{1, 2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
};
int *num_ptr;
num_ptr = &num[0][0];

PrintA( num_ptr, 5 );
}

void PrintA( int *a, int cols )
{
for (int r = 0; r < 3 ; r++)
{for ( int c = 0; c < 5; c++ )
cout << *(*(a + r) + c) << " ";
cout << endl;
}
}
 
S

Siemel Naran

Super KHB said:
void PrintA( int *a, int cols )
{
for (int r = 0; r < 3 ; r++)
{for ( int c = 0; c < 5; c++ )
cout << *(*(a + r) + c) << " ";
cout << endl;
}
}

Should the line
cout << *(*(a + r) + c) << " ";

be

cout << *(*(a + 5*r) + c) << " ";


Also, instead of the hardcoded constant "5", do you mean to use the variable
"cols"?
 
K

Karl Heinz Buchegger

Super said:
I know that I am close, but it wont work. HELP!!!!

void PrintA( int * , int);

void main()
{

int num[3][5] = {
{1, 2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
};

Here you have a memory layout looking like this:

num
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

and the compiler keeps track of calulating the correct offset from the beginning
when given 2 indices
int *num_ptr;
num_ptr = &num[0][0];

PrintA( num_ptr, 5 );
}

void PrintA( int *a, int cols )
{
for (int r = 0; r < 3 ; r++)
{for ( int c = 0; c < 5; c++ )
cout << *(*(a + r) + c) << " ";

Here you assume a memory layout like this:

num
+------+ +----+----+----+----+----+
| o--------->| 1 | 2 | 3 | 4 | 5 |
| | +----+----+----+----+----+
+------+ +----+----+----+----+----+
| o------------------>| 6 | 7 | 8 | 9 | 10 |
| | +----+----+----+----+----+
+------+ +----+----+----+----+----+
| o------------>| 11 | 12 | 13 | 14 | 15 |
| | +----+----+----+----+----+
+------+

( an array of pointers, where each pointer points to an array of int )

Totally different.
 
O

Old Wolf

Siemel Naran said:
Should the line
be
cout << *(*(a + 5*r) + c) << " ";

We are dealing with a pointer to int so there should be
exactly one indirection. I would write it as:
a[r * cols + c]

(NB. Actually I wouldn't write this because I think
it is UB. However we have just had a big discussion
with no conclusion on that topic in another thread
so I don't want to start it up again here).
 
S

Siemel Naran

Oops, that should be

cout << *(a + 5*r + c) << " ";
We are dealing with a pointer to int so there should be
exactly one indirection. I would write it as:
a[r * cols + c]

Thanks for pointing out.
(NB. Actually I wouldn't write this because I think
it is UB. However we have just had a big discussion
with no conclusion on that topic in another thread
so I don't want to start it up again here).

What is UB?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top