Talk About Network

Google





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

Re: Point to a two-dimension array

by Richard Heathfield <rjh@[EMAIL PROTECTED] > Apr 11, 2008 at 07:05 AM

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
 




 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
localhost-V2008-12-19 Wed Jan 7 10:18:13 PST 2009.