Pointer to array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Pointer to array

 
0
  #1
Jun 7th, 2007
How can I make a pointer to an array? So that I access the members of the array by using the pointer in a seperate function. I'm wondering because I have an array I have to access in functions other than the one it was defined in, but I need it to be defined in that function, and not globally, because the size is dependant on the user input...

Which is how I came to the pointer to the array idea... that should let me access the data in other functions right? If so, how do I do that and then access the objects from the pointer?

Thanks,
-Matt
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pointer to array

 
0
  #2
Jun 7th, 2007
No need for a pointer. Just keep on passing the array name wherever you like, a pointer to the array would be automatically passed since the array name is the pointer to its first element.
  1. void createArray()
  2. {
  3. int* myArr = new int[10];
  4. useArray(myArr, 10);
  5. }
  6.  
  7. void useArray(int arr[], int sz)
  8. {
  9. for(int i = 0; i < sz; ++i)
  10. arr[i] = 0;
  11. }
Last edited by ~s.o.s~; Jun 7th, 2007 at 3:26 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Pointer to array

 
0
  #3
Jun 7th, 2007
I understand what oyu mean, but not how to really use it the way you mean to use it, how could I apply it to this code, I need to access the jailHouse array in main() and probably other functions to come...
  1. /* ISU program created by Matt
  2.   in grade 12 programming. */
  3. #include <iostream>
  4. #include <list>
  5. #include <iterator>
  6. #include <vector>
  7. using namespace std;
  8. class humanPlayer
  9. {
  10. public:
  11. humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum);
  12. ~humanPlayer(){};
  13.  
  14. void addToList(unsigned int countryToAdd) {countriesOwned.push_back(countryToAdd);}
  15. void showList();
  16. void removeFromList(unsigned int countryToRemove) {countriesOwned.remove(countryToRemove);}
  17. void checkIfInList(unsigned int countryToCheck);
  18.  
  19. string getPlayerName() {return playerName;}
  20. int getPlayerTurnNum() {return playerTurnNum;}
  21. void setPlayerTurnNum(unsigned int newPlayerTurnNum);
  22.  
  23. private:
  24. list<int> countriesOwned;
  25. string playerName;
  26. unsigned int playerTurnNum;
  27. };
  28. humanPlayer::humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum)
  29. {
  30. playerName = thePlayersName;
  31. playerTurnNum = thePlayersTurnNum;
  32. }
  33.  
  34. void humanPlayer::checkIfInList(unsigned int countryToCheck)
  35. {
  36. if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
  37. {
  38. cout << countryToCheck << " is in the list.\n";
  39. }
  40. else
  41. {
  42. cout << countryToCheck << " is NOT in the list.\n";
  43. }
  44. }
  45.  
  46. void humanPlayer::showList()
  47. {
  48. copy(countriesOwned.begin(),countriesOwned.end(),ostream_iterator<int>(cout,", "));
  49. cout << endl;
  50. }
  51.  
  52. void humanPlayer::setPlayerTurnNum(unsigned int newPlayerTurnNum)
  53. {
  54. playerTurnNum = newPlayerTurnNum;
  55. }
  56.  
  57. void getName();
  58. unsigned int numOfPlayers;
  59.  
  60. int main()
  61. {
  62. getName();
  63.  
  64. system("PAUSE");
  65. }
  66.  
  67. void getName()
  68. {
  69. cout << "How many players will there be? ";
  70. cin >> numOfPlayers;
  71. cout << endl;
  72.  
  73. humanPlayer *jailHouse[numOfPlayers];
  74.  
  75. for(unsigned int i=0 ; i<numOfPlayers ; i++)
  76. {
  77. char name[256];
  78. cin.ignore(255,'\n');
  79. cout << "What is player " << (i+1) << "'s name? ";
  80. cin.get(name,256);
  81.  
  82. jailHouse[i] = new humanPlayer(name, (i+1));
  83. }
  84.  
  85. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Pointer to array

 
0
  #4
Jun 7th, 2007
Create the initial array (humanPlayer *jailHouse[numOfPlayers]) inside of main(). Then pass 'jailHouse' as an argument to getName().
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Pointer to array

 
0
  #5
Jun 7th, 2007
But you don't find out what the numOfPlayers value is until getName() is started.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Pointer to array

 
0
  #6
Jun 7th, 2007
>But you don't find out what the numOfPlayers value is until getName() is started.
Sorry, I didn't read your code very carefully.

Basically make the entire array dynamic.
  1. int main() {
  2. humanPlayer **jailHouse;
  3. ...
Then when you pass it to getName(), use a reference so that the base address is not lost.
  1. void getName(humanPlayer **&jailHouse) {
  2. ...
  3. // do all your allocation here
  4. }
Last edited by John A; Jun 7th, 2007 at 10:05 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pointer to array

 
0
  #7
Jun 7th, 2007
In such a scenario, its always recommended that the function 'getName()' return an newly created array instead of incurring the pain of creating an array variable and passing it around.
Last edited by ~s.o.s~; Jun 7th, 2007 at 10:23 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Pointer to array

 
0
  #8
Jun 7th, 2007
Originally Posted by ~s.o.s~ View Post
In such a scenario, its always recommended that the function 'getName()' return an newly created array instead of incurring the pain of creating an array variable and passing it around.
Meh, whatever. But if the OP does that, they'll have to make sure that humanPlayer *jailHouse[numOfPlayers]; is static.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Pointer to array

 
0
  #9
Jun 7th, 2007
Ok, after reading these posts, I'm not sure what I should do for my code. Should I do what joe said to do with the reference? Or what s.o.s. was saying to do creating a new array and what not?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Pointer to array

 
0
  #10
Jun 8th, 2007
>Or what s.o.s. was saying to do creating a new array and what not?
Probably do what ~s.o.s~ said, he knows better than me. ;-)

But whatever you do, don't forget the static keyword! It's very important if you don't want the memory that you allocate to just disappear when the function returns.
"Technological progress is like an axe in the hands of a pathological criminal."
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