473,513 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
}
}
Jul 22 '05 #1
4 2382
"Super KHB" <lo**@message.end> wrote in message news:Jx0gd.49083
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"?
Jul 22 '05 #2
Super KHB wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
> cout << *(*(a + r) + c) << " ";

try this cout << *(a+((cols * r) + c))<< " ";
Jul 22 '05 #4
"Siemel Naran" <Si*********@REMOVE.att.net> wrote:
"Super KHB" <lo**@message.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).
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
3897
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
8
2239
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
42
5856
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is...
36
2799
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
24
2153
by: arnuld | last post by:
PURPOSE: see the comments. WHAT I GOT: infinite loop /* This program will simply create an array of pointers to integers * and will fill it with some values while using malloc...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.