1.11M Members

Pointer to array

 
0
 

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

 
0
 

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;
}
 
0
 

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...

/* ISU program created by Matt 
   in grade 12 programming. */
#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();
unsigned int numOfPlayers;
 
int main()
     {
     getName();
 
     system("PAUSE");
     }  
 
void getName()
     {
     cout << "How many players will there be? ";
     cin >> numOfPlayers;
     cout << endl;
 
     humanPlayer *jailHouse[numOfPlayers];
 
     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[i] = new humanPlayer(name, (i+1));
          }     
 
     }
 
0
 

Create the initial array ( humanPlayer *jailHouse[numOfPlayers] ) inside of main(). Then pass 'jailHouse' as an argument to getName().

 
0
 

But you don't find out what the numOfPlayers value is until getName() is started.

 
0
 

>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
}
 
0
 

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.

 
0
 

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.

 
0
 

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?

 
0
 

>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.

 
0
 

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 ;
}
 
0
 

> Probably do what ~s.o.s~ said, he knows better than me. ;-)
You are too kind my friend. ;-)

 
0
 

Is there any way to use STL lists for this? Because then I wouldn't need to specify a size at the begining and could make it a global list. Because I'll probably have a coupel of these things in the program and passing them to each function will become confusing.

 
0
 

Try out the approach provided by thekashyap in post #11.

 
0
 

Shouldn't this work? Why does it say no matching function?

/* ISU program created by Matt 
   in grade 12 programming. */
#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();
unsigned int numOfPlayers;
vector<humanPlayer> jailHouse;
int main()
     {
     getName();
 
     system("PAUSE");
     }  
 
void getName()
     {
     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)));
          }     
 
     }
 
0
 

Also, I tried what was suggesed in that post and it didn't seem to work for me too well.

Question Answered as of 7 Years Ago by John A, ~s.o.s~ and thekashyap
 
0
 

Ok, I figured out it does work, but you need to get rid of the word "new" in front of humanPlayer.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article