942,956 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 78098
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 7th, 2007
0

Pointer to array

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Jun 7th, 2007
0

Re: Pointer to array

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.
C++ Syntax (Toggle Plain Text)
  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.
Super Moderator
Featured Poster
Reputation Points: 3217
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,870 posts
since Jun 2006
Jun 7th, 2007
0

Re: Pointer to array

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...
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Jun 7th, 2007
0

Re: Pointer to array

Create the initial array (humanPlayer *jailHouse[numOfPlayers]) inside of main(). Then pass 'jailHouse' as an argument to getName().
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jun 7th, 2007
0

Re: Pointer to array

But you don't find out what the numOfPlayers value is until getName() is started.
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Jun 7th, 2007
0

Re: Pointer to array

>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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jun 7th, 2007
0

Re: Pointer to array

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.
Super Moderator
Featured Poster
Reputation Points: 3217
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,870 posts
since Jun 2006
Jun 7th, 2007
0

Re: Pointer to array

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jun 7th, 2007
0

Re: Pointer to array

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?
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Jun 8th, 2007
0

Re: Pointer to array

>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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

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: is this a form of piping in c++?
Next Thread in C++ Forum Timeline: Simulate Mouse Move





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


Follow us on Twitter


© 2011 DaniWeb® LLC