|
Prev: Back to School with Skybuck Flying: Lesson 1, Integer Basics.
Next: Back to School with NoName: Lesson 2, Integer Range Checking
From: cplusplusquestion on 11 Apr 2008 02:25 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; it does not work. Any good idea?
From: Richard Heathfield on 11 Apr 2008 03:05 cplusplusquestion(a)gmail.com 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@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999
From: Barry Schwarz on 12 Apr 2008 00:47 On Thu, 10 Apr 2008 23:25:52 -0700 (PDT), cplusplusquestion(a)gmail.com 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; 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
From: Bart van Ingen Schenau on 18 Apr 2008 09:00
Barry Schwarz wrote: > On Thu, 10 Apr 2008 23:25:52 -0700 (PDT), cplusplusquestion(a)gmail.com > 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.parashift.com/c++-faq-lite/ |