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/


|