 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Super KHB Guest
|
Posted: Thu Oct 28, 2004 6:42 am Post subject: Printing 2D Arrays with Pointers |
|
|
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;
}
}
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Thu Oct 28, 2004 7:12 am Post subject: Re: Printing 2D Arrays with Pointers |
|
|
"Super KHB" <look (AT) message (DOT) end> wrote
| Quote: | 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
| Quote: | 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"?
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Thu Oct 28, 2004 9:44 am Post subject: Re: Printing 2D Arrays with Pointers |
|
|
Super KHB wrote:
| Quote: |
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
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| Quote: | 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
| Quote: | 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
+------+ +----+----+----+----+----+
| Quote: | 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.
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Venki Guest
|
Posted: Thu Oct 28, 2004 11:35 am Post subject: Re: Printing 2D Arrays with Pointers |
|
|
| Quote: | cout << *(*(a + r) + c) << " ";
|
try this cout << *(a+((cols * r) + c))<< " ";
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Thu Oct 28, 2004 7:46 pm Post subject: Re: Printing 2D Arrays with Pointers |
|
|
"Siemel Naran" <SiemelNaran (AT) REMOVE (DOT) att.net> wrote:
| Quote: | "Super KHB" <look (AT) message (DOT) end> wrote:
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) << " ";
|
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).
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Fri Oct 29, 2004 7:32 am Post subject: Re: Printing 2D Arrays with Pointers |
|
|
"Old Wolf" <oldwolf (AT) inspire (DOT) net.nz> wrote in message
| Quote: | "Siemel Naran" <SiemelNaran (AT) REMOVE (DOT) att.net> wrote:
Should the line
cout << *(*(a + r) + c) << " ";
be
cout << *(*(a + 5*r) + c) << " ";
|
Oops, that should be
cout << *(a + 5*r + c) << " ";
| Quote: | 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.
| Quote: | (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?
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|