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?
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.
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...
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.
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.
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?
>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.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.