| | |
Pointer to array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
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
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)
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; }
Last edited by ~s.o.s~; Jun 7th, 2007 at 3:26 pm.
I don't accept change; I don't deserve to live.
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)
/* 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)); } }
>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.
Then when you pass it to getName(), use a reference so that the base address is not lost.
Sorry, I didn't read your code very carefully.
Basically make the entire array dynamic.
C++ Syntax (Toggle Plain Text)
int main() { humanPlayer **jailHouse; ...
C++ Syntax (Toggle Plain Text)
void getName(humanPlayer **&jailHouse) { ... // do all your allocation here }
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."
•
•
•
•
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.
humanPlayer *jailHouse[numOfPlayers]; is static. "Technological progress is like an axe in the hands of a pathological criminal."
>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
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."
![]() |
Similar Threads
- pointer and array arithmetic (C)
- Declaration of dynamic pointer array puzzle. (C)
- Please help with pointer to array of pointers (C++)
- structures containing a pointer to an array (C++)
- Array limit (C)
Other Threads in the C++ Forum
- Previous Thread: Small project in C++
- Next Thread: Simulate Mouse Move
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






