944,214 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7325
  • C++ RSS
Oct 27th, 2007
0

pointers and multi-dimensional arrays

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AnthIste is offline Offline
11 posts
since Oct 2007
Oct 27th, 2007
0

Re: pointers and multi-dimensional arrays

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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 27th, 2007
0

Re: pointers and multi-dimensional arrays

Examples
c++ Syntax (Toggle Plain Text)
  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;
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 27th, 2007
0

Re: pointers and multi-dimensional arrays

Thanks people!! You guys are awesome!
Back to the drawing board...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AnthIste is offline Offline
11 posts
since Oct 2007
Oct 28th, 2007
0

Re: pointers and multi-dimensional arrays

hmm... just one more thing, if
C++ Syntax (Toggle Plain Text)
  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 []?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AnthIste is offline Offline
11 posts
since Oct 2007
Oct 28th, 2007
0

Re: pointers and multi-dimensional arrays

Take a look at this:


cpp Syntax (Toggle Plain Text)
  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. }
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 28th, 2007
0

Re: pointers and multi-dimensional arrays

shot. il go disect that now now and try make sense of it all

cheers and thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AnthIste is offline Offline
11 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help on overload method %
Next Thread in C++ Forum Timeline: Two Player Craps





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC