User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,577 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,634 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1692 | Replies: 6 | Solved
Reply
Join Date: Oct 2007
Location: south africa
Posts: 11
Reputation: AnthIste is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
AnthIste's Avatar
AnthIste AnthIste is offline Offline
Newbie Poster

pointers and multi-dimensional arrays

  #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 :(
=======================
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: pointers and multi-dimensional arrays

  #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  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: pointers and multi-dimensional arrays

  #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  
Join Date: Oct 2007
Location: south africa
Posts: 11
Reputation: AnthIste is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
AnthIste's Avatar
AnthIste AnthIste is offline Offline
Newbie Poster

Re: pointers and multi-dimensional arrays

  #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  
Join Date: Oct 2007
Location: south africa
Posts: 11
Reputation: AnthIste is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
AnthIste's Avatar
AnthIste AnthIste is offline Offline
Newbie Poster

Re: pointers and multi-dimensional arrays

  #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  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: pointers and multi-dimensional arrays

  #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  
Join Date: Oct 2007
Location: south africa
Posts: 11
Reputation: AnthIste is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
AnthIste's Avatar
AnthIste AnthIste is offline Offline
Newbie Poster

Re: pointers and multi-dimensional arrays

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC