![]() |
|
Accesing 2d arrays with pointer notation |
Paul whoknows
Member #5,081
September 2004
![]() |
As all of us already know -> array[4] == *(array+4) /* BUT */ array[4][4] == ????? If I have a 2d array like this: array[4][4], how can I access to its elements with pointers notation? ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
kazzmir
Member #1,786
December 2001
![]() |
Well just try it out:
Produces this: Foo = 0xbffff3f0 Foo[0] = 0xbffff3f0 Foo[1] = 0xbffff3f5 Foo[2] = 0xbffff3fa Foo[3] = 0xbffff3ff Foo[0][0] = 0xbffff3f0 Foo[0][1] = 0xbffff3f1 Foo[0][2] = 0xbffff3f2 Foo[0][3] = 0xbffff3f3 Foo[0][4] = 0xbffff3f4 Foo[0][5] = 0xbffff3f5 You can see that the second index into the first array is exactly N bytes away where N = sizeof(type) * elements in the second array. |
Paul whoknows
Member #5,081
September 2004
![]() |
but how can I access to its elements with pointer notation? ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Kitty Cat
Member #2,815
October 2002
![]() |
int array[h][w]; array[y][x] == *(array + y*w + x);
-- |
Carrus85
Member #2,633
August 2002
![]() |
Edit: Sorry, this was a wrong response...
|
Kitty Cat
Member #2,815
October 2002
![]() |
If it's an int **array; that's what you do. But int array[][] isn't the same because it's all one contiguous block in memory. The only pointer is the implicit ones the compiler gives you. int main() { int array[1][1]; printf("&array = %p\n&array[0] = %p\n&array[0][0] = %p\n", &array, &array[0], &array[0][0]); return 0; } gives (with added spacing for ease-of-reading): &array = 0xbfb44f64 &array[0] = 0xbfb44f64 &array[0][0] = 0xbfb44f64
-- |
Paul whoknows
Member #5,081
September 2004
![]() |
Thank you Kitticat, that was really helpful!!! Well I really suck at strings manipulations, look a this
I get an error in the marked line, What the heck am I doing wrong?
____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
ReyBrujo
Moderator
January 2001
![]() |
I refuse to help anyone using conio.h Just teasing. You should be using something like printf("\nName :%s",(char *)(nya[0]));, maybe printf("\nName :%s",(char *)(&nya[0]));, although I am not sure which one is correct. -- |
Kitty Cat
Member #2,815
October 2002
![]() |
You're passing a char, but it's expecting a pointer to a char. You'd pass nya[0] (or it looks like you meant nya[i]), which will give a pointer to the string. -- |
Paul whoknows
Member #5,081
September 2004
![]() |
Master Rey, you know all the answers as usual!!! Quote: You're passing a char, but it's expecting a pointer to a char. You'd pass nya(0) (or it looks like you meant nya(i)), which will give a pointer to the string. Well, so I have to omit the second index, now I think pointer notation is easier than indexes:P EDIT: isn't a bit confusing? now I really think pointer notation is easier. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
LennyLen
Member #5,313
December 2004
![]() |
Quote: &nya[5][5] is a pointer to a char(a 2d pointer?) Not really. This is no different than in the following snippet: int *ptr, value; ptr = &value; ptr is the pointer. We can point ptr at value though by assigning the address of value (&value) to it. Similarly, you can assign &nya[5][5] to a pointer. Or &nya[5] (or even &nya). Quote:
nya[5][5] is a char If you think of nya[5] as not just being a pointer to a char, but a pointer to an array of chars, it makes more sense.
|
Paul whoknows
Member #5,081
September 2004
![]() |
Finally finished, I didn't follow KittiCat way exactly, but it works the same.
EDIT: I NEED HELP I am getting 2 warinings, I am using Borland C++5.0 compiler (win16 console app), but a friend of mine obtained 2 errors instead, compiling with Borland C++5.2. These are the lines: ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
A J
Member #3,025
December 2002
![]() |
Dont use pointers if you dont have to. ___________________________ |
Andrei Ellman
Member #3,434
April 2003
|
Kitty Cat said:
int array[h][w]; what about array[y][x] == *( (*(array + y)) + x); instead? AE. -- |
Kitty Cat
Member #2,815
October 2002
![]() |
array[y][x] == *( (*(array + y)) + x); -- |
Paul whoknows
Member #5,081
September 2004
![]() |
Quote:
I am getting 2 warinings . . .
char *nya = _nya; /* wrong */ char *nya = _nya[0]; /* right */ Fixed! Quote:
Dont use pointers if you dont have to. We are forced to use pointer notation in our exams, if we don't, we get disqualified. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Andrei Ellman
Member #3,434
April 2003
|
I always thought that a 2D array was a pointer to an array of pointers into the data. I've written some code that allocates dynamic 2D arrays. First, it allocates a block of memory equivalent to ysize*xsize*datasize, and then it allocates a separate 1D array equivalent to ysize*sizeof(void). This separate array is filled to point to each row of xsize*datasize bytes. The resulting array can be accessed using normal 2D array notation. AE. -- |
Kitty Cat
Member #2,815
October 2002
![]() |
A pointer is a pointer , and an array is an array. int **ptr; is a pointer to a pointer, and int array[foo][bar]; is an array of foo*bar elements. A pointer can point to an array, but a pointer is not an array. -- |
|