DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   pointers and multi-dimensional arrays (http://www.daniweb.com/forums/thread94517.html)

AnthIste Oct 27th, 2007 3:04 pm
pointers and multi-dimensional arrays
 
hey there. my problem is pretty simple (i think). I dont know how to point at a multi dimensional array.how i understand normal array pointers is:

int numbers[5];
int *p = numbers;

//so now *p points to numbers[0]
//so if you say:

*p = 10;

//is the same as saying numbers[0] = 10;

//now by saying:

p++;
*p = 20;

//you are basically saying numbers[1] = 20;

right. thats fine. in my mind thats how it works and it makes me happy. like I said, im a newb. So if i totally misunderstand the workings of pointers please enlighten me. BUT now if i say:
int numbers[4][2];
int *p = numbers;

i get an error that tells me:
"cannot convert `std::string (*)[2]' to `std::string*' in initialization "

so what must i do???
the reason why i want to point to multi-dimensional arrays is basically because i have a [4][4] grid of type string that displays tiles in a 10x10 grid on-screen, like so:

####
#### y
####
####
x

I want to add another type of char to a specific coordinate on THAT SPECIFIC grid, using a function, which is why I am using pointers.

####
#### y
##@#
####
x

but i get an error message :(. hope that explanation helps

thanks

twomers Oct 27th, 2007 3:11 pm
Re: pointers and multi-dimensional arrays
 
You can point it to the first y position.

int *p = numbers[0];

Or whichever one you want.

Salem Oct 27th, 2007 4:41 pm
Re: pointers and multi-dimensional arrays
 
Examples
int numbers[4][2];
int (*p)[2] = numbers;

(*p)[0] = 0;  // numbers[0][0] = 0;

p++; // move to next row
(*p)[0] = 0;  // numbers[1][0] = 0;

AnthIste Oct 27th, 2007 5:25 pm
Re: pointers and multi-dimensional arrays
 
Thanks people!! You guys are awesome!
Back to the drawing board...

AnthIste Oct 28th, 2007 9:55 am
Re: pointers and multi-dimensional arrays
 
hmm... just one more thing, if
(*p)[0] = 0; // numbers[1][0] = 0;
refers to numbers[1][0]

then how do you refer to numbers[0][3] for instance? like not the first [], but the second []?

twomers Oct 28th, 2007 11:51 am
Re: pointers and multi-dimensional arrays
 
Take a look at this:


int main() {
  int numbers[4][4] = { { 0, 1, 2, 3 },
                        { 4, 5, 6, 7 },
                        { 0, 9, 8, 7 },
                        { 6, 5, 4, 3 } };
  int (*p)[4] = numbers;

  (*p)[0] = 42;
  p++;
  (*p)[3] = 63;

  for ( int y=0, x; y<4; y++ ) {
    for ( x=0; x<4; x++ )
      std::cout<< numbers[y][x] << " ";
    std::cout<< "\n";
  }

  return 0;
}

AnthIste Oct 28th, 2007 4:42 pm
Re: pointers and multi-dimensional arrays
 
shot. il go disect that now now and try make sense of it all

cheers and thanks :D


All times are GMT -4. The time now is 11:53 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC