| | |
Pointer to array
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.
Sacrifice is a painful, pure and beautiful thing.
Sacrifice is a painful, pure and beautiful thing.
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."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
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.
I don't accept change; I don't deserve to live.
Sacrifice is a painful, pure and beautiful thing.
Sacrifice is a painful, pure and beautiful thing.
•
•
•
•
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."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
>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."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
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
Views: 32699 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy lib link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ video visual visualstudio win32 window windows winsock wordfrequency wxwidgets






