Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Re: Point to a ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 4103 of 4306
Post > Topic >>

Re: Point to a two-dimension array

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > Apr 18, 2008 at 03:00 PM

Barry Schwarz wrote:

> On Thu, 10 Apr 2008 23:25:52 -0700 (PDT), cplusplusquestion@[EMAIL PROTECTED]
> wrote:
> 
>>There is a two-dimensional array:
>>
>>int grades[MAX][MAX];
>>for( i = 0; i < MAX; i++)
>>   for( j = 0; j < MAX; j++)
>>      grades[i][j] = -1;
>>
>>I would like to assign an another variable to this array, for example:
>>
>>int another_grades[MAX][MAX];
>>for( i = 0; i < MAX; i++)
>>   for( j = 0; j < MAX; j++)
>>      another_grades[i][j] = grades[i][j];
>>
>>Here, I need to declare another array.  Is it possible to have a
>>pointer to point grades[MAX][MAX]?  As I've tried:
>>
>>int** another_g = grades;
> 
<snip - explanation> 
> Since it is a bit unusual to want to deal with the array as a whole,
> you might be better served with
> int *another_g = &grades[0][0];
> which resolves to the same address but with a more usable type.

Just a note of caution on that technique.
First, you should make sure that every element in grades is properly
initialised. Otherwise, when you iterate through the another_g array,
you run a big risk of data corruption by using uninitialised (random)
values in your calculation.

And secondly, a bounds-checking implementation may decide that another_g
only has MAX elements and it may deny you access to the elements of
grades[1][0] and further.

Bart v Ingen Schenau
-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.para****ft.com/c++-faq-lite/
 




 4 Posts in Topic:
Point to a two-dimension array
cplusplusquestion@[EMAIL   2008-04-10 23:25:52 
Re: Point to a two-dimension array
Richard Heathfield <rj  2008-04-11 07:05:20 
Re: Point to a two-dimension array
Barry Schwarz <schwarz  2008-04-11 21:47:42 
Re: Point to a two-dimension array
Bart van Ingen Schenau &l  2008-04-18 15:00:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 13:00:50 CDT 2008.