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 3 of 4 Topic 4103 of 4400
Post > Topic >>

Re: Point to a two-dimension array

by Barry Schwarz <schwarzb@[EMAIL PROTECTED] > Apr 11, 2008 at 09:47 PM

On Thu, 10 Apr 2008 23:25:52 -0700 (PDT), cplusplusquestion@[EMAIL PROTECTED]
>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;

The expression grades has an array type.  Since this is not one of the
exceptions, the expression is automatically converted to the address
of the first element of the array with type pointer to element type.
In other words, the expression grades is treated exactly the same as
&grades[0].  Since grades[0] is itself an array of MAX int, what you
need is
	int (*another_g)[MAX] = grades;

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.

>
>it does not work.  Any good idea?


Remove del for email
 




 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 12:50:51 PST 2009.