cplusplusquestion@[EMAIL PROTECTED]
said:
> 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.
That's easy: int yet_another_grades[MAX][MAX];
> Is it possible to have a
> pointer to point grades[MAX][MAX]?
Yes. Although it is possible to do this without typedef, typedef really
helps here:
typedef int my_grade_array_type[MAX][MAX];
my_grade_array_type *my_pointer_to_array = &grades;
But I can't help thinking that this isn't really what you want.
> As I've tried:
>
> int** another_g = grades;
>
> it does not work. Any good idea?
Yes - a good idea is to stop thinking that arrays are just a kind of
pointer. They aren't. They are a completely different type. You can't just
trade off *s for []s in your declarations.
Now, what I *think* you are really trying to do is point to each element
in
the array in turn. You might, for instance, be trying to add up all the
grades. You can do this with indices, of course, but you can also do it
with pointers:
int *p;
int i;
int j;
int sum = 0;
for(i = 0; i < MAX; i++)
{
p = grade[i];
for(j = 0; j < MAX; j++)
{
sum += *p++;
}
}
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www.
+rjh@[EMAIL PROTECTED]
users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


|