954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pointer to array

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

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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));
          }     
 
     }
Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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

John A
Vampirical Lurker
Team Colleague
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.

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

>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
Team Colleague
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
Administrator
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
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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?

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

>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
Team Colleague
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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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.

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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)));
          }     
 
     }
Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You