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.
void createArray()
{
int* myArr = new int[10];
useArray(myArr, 10);
}
void useArray(int arr[], int sz)
{
for(int i = 0; i < sz; ++i)
arr[i] = 0;
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Create the initial array ( humanPlayer *jailHouse[numOfPlayers] ) inside of main(). Then pass 'jailHouse' as an argument to getName().
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>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.
int main() {
humanPlayer **jailHouse;
...
Then when you pass it to getName(), use a reference so that the base address is not lost.
void getName(humanPlayer **&jailHouse) {
...
// do all your allocation here
}
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Just wondering that after #including vector/list etc, why are you using C-style array for this? Use C++ array (i.e. vector) instead, life is so much more easy.
#include <iostream>
#include <list>
#include <iterator>
#include <vector>
using namespace std;
class humanPlayer
{
public:
humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum);
~humanPlayer(){};
void addToList(unsigned int countryToAdd) {countriesOwned.push_back(countryToAdd);}
void showList();
void removeFromList(unsigned int countryToRemove) {countriesOwned.remove(countryToRemove);}
void checkIfInList(unsigned int countryToCheck);
string getPlayerName() {return playerName;}
int getPlayerTurnNum() {return playerTurnNum;}
void setPlayerTurnNum(unsigned int newPlayerTurnNum);
private:
list<int> countriesOwned;
string playerName;
unsigned int playerTurnNum;
};
humanPlayer::humanPlayer(string thePlayersName, unsigned int thePlayersTurnNum)
{
playerName = thePlayersName;
playerTurnNum = thePlayersTurnNum;
}
void humanPlayer::checkIfInList(unsigned int countryToCheck)
{
if(find(countriesOwned.begin(), countriesOwned.end(), countryToCheck) != countriesOwned.end())
{
cout << countryToCheck << " is in the list.\n";
}
else
{
cout << countryToCheck << " is NOT in the list.\n";
}
}
void humanPlayer::showList()
{
copy(countriesOwned.begin(),countriesOwned.end(),ostream_iterator<int>(cout,", "));
cout << endl;
}
void humanPlayer::setPlayerTurnNum(unsigned int newPlayerTurnNum)
{
playerTurnNum = newPlayerTurnNum;
}
void getName(vector<humanPlayer>& jailHouse);
void print_it(const vector<humanPlayer>& jailHouse) ;
//unsigned int numOfPlayers; //not needed, use jailHouse.size() instead.
int main()
{
vector<humanPlayer> jailHouse ;
getName(jailHouse);
print_it(jailHouse) ;
system("PAUSE");
}
void getName(vector<humanPlayer>& jailHouse)
{
cout << "How many players will there be? ";
cin >> numOfPlayers;
cout << endl;
for(unsigned int i=0 ; i<numOfPlayers ; i++)
{
char name[256];
cin.ignore(255,'\n');
cout << "What is player " << (i+1) << "'s name? ";
cin.get(name,256);
jailHouse.push_back( new humanPlayer(name, (i+1)) );
}
}
void print_it(const vector<humanPlayer>& jailHouse)
{
for( int i = 0; i < jailHouse.size(); i++ )
cout << i << " = " << jailHouse[i].getPlayerName().c_str() << endl ;
}
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
> Probably do what ~s.o.s~ said, he knows better than me. ;-)
You are too kind my friend. ;-)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Try out the approach provided by thekashyap in post #11.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734