pointers and multi-dimensional arrays

Thread Solved
Reply

Join Date: Oct 2007
Posts: 11
Reputation: AnthIste is an unknown quantity at this point 
Solved Threads: 0
AnthIste's Avatar
AnthIste AnthIste is offline Offline
Newbie Poster

pointers and multi-dimensional arrays

 
0
  #1
Oct 27th, 2007
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:

  1. int numbers[5];
  2. int *p = numbers;
  3.  
  4. //so now *p points to numbers[0]
  5. //so if you say:
  6.  
  7. *p = 10;
  8.  
  9. //is the same as saying numbers[0] = 10;
  10.  
  11. //now by saying:
  12.  
  13. p++;
  14. *p = 20;
  15.  
  16. //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:
  1. int numbers[4][2];
  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
=======================
I CANT GET AN IMAGE IN MY SIG :(
=======================
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,839
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 49
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: pointers and multi-dimensional arrays

 
0
  #2
Oct 27th, 2007
You can point it to the first y position.

int *p = numbers[0];

Or whichever one you want.
Last edited by twomers; Oct 27th, 2007 at 3:12 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: pointers and multi-dimensional arrays

 
0
  #3
Oct 27th, 2007
Examples
  1. int numbers[4][2];
  2. int (*p)[2] = numbers;
  3.  
  4. (*p)[0] = 0; // numbers[0][0] = 0;
  5.  
  6. p++; // move to next row
  7. (*p)[0] = 0; // numbers[1][0] = 0;
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: AnthIste is an unknown quantity at this point 
Solved Threads: 0
AnthIste's Avatar
AnthIste AnthIste is offline Offline
Newbie Poster

Re: pointers and multi-dimensional arrays

 
0
  #4
Oct 27th, 2007
Thanks people!! You guys are awesome!
Back to the drawing board...
=======================
I CANT GET AN IMAGE IN MY SIG :(
=======================
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: AnthIste is an unknown quantity at this point 
Solved Threads: 0
AnthIste's Avatar
AnthIste AnthIste is offline Offline
Newbie Poster

Re: pointers and multi-dimensional arrays

 
0
  #5
Oct 28th, 2007
hmm... just one more thing, if
  1. (*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 []?
=======================
I CANT GET AN IMAGE IN MY SIG :(
=======================
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,839
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 49
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: pointers and multi-dimensional arrays

 
0
  #6
Oct 28th, 2007
Take a look at this:


  1. int main() {
  2. int numbers[4][4] = { { 0, 1, 2, 3 },
  3. { 4, 5, 6, 7 },
  4. { 0, 9, 8, 7 },
  5. { 6, 5, 4, 3 } };
  6. int (*p)[4] = numbers;
  7.  
  8. (*p)[0] = 42;
  9. p++;
  10. (*p)[3] = 63;
  11.  
  12. for ( int y=0, x; y<4; y++ ) {
  13. for ( x=0; x<4; x++ )
  14. std::cout<< numbers[y][x] << " ";
  15. std::cout<< "\n";
  16. }
  17.  
  18. return 0;
  19. }
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: AnthIste is an unknown quantity at this point 
Solved Threads: 0
AnthIste's Avatar
AnthIste AnthIste is offline Offline
Newbie Poster

Re: pointers and multi-dimensional arrays

 
0
  #7
Oct 28th, 2007
shot. il go disect that now now and try make sense of it all

cheers and thanks
=======================
I CANT GET AN IMAGE IN MY SIG :(
=======================
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC