943,706 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1520
  • C++ RSS
Oct 21st, 2008
0

C++ Array pointer problem

Expand Post »
I'm writing a program wich is using an array -> startpoint[4]
this array will be later filled by values from an other array. this is not the problem!

the problem begins here:

I have a class: computerPlayer.cpp
with a method: getStartpoint()

cpp Syntax (Toggle Plain Text)
  1. int *startPoint[4]; // global variable
  2.  
  3. int* computerPlayer::getStartpoint(){
  4.  
  5. int beginpunta = 25; // static value -> will be changed later
  6. int beginpuntb= 5; // static value -> will be changed later
  7. int beginpuntc = 50;// static value -> will be changed later
  8.  
  9. startPoint[0] = &beginpunta; // set array[0] with the value found on adres of beginpunta
  10. startPoint[1] = &beginpuntb; // set array[0] with the value found on adres of beginpuntb
  11. startPoint[2] = &beginpuntc; // set array[0] with the value found on adres of beginpuntc
  12.  
  13. cout << *startPoint[0] << " Pos 0" << endl; // print out value on location startPoint[0]
  14. cout << *startPoint[1] << " Pos 1" << endl; // print out value on location startPoint[1]
  15. cout << *startPoint[2] << " Pos 2" << endl; // print out value on location startPoint[2]
  16.  
  17. return *startPoint; // give the pointer to the caller of this function
  18. }

the cout works properly according to aspected values:

line 1: 25 Pos 0
line 2: 5 Pos 1
line 3: 50 Pos 2

this method is called from the main method in an other file: Artificial Intelligence.cpp

with in the Artificial Intelligence.cpp:

cpp Syntax (Toggle Plain Text)
  1.  
  2. computerPlayer p(3,3,3); // creation of computerplayer
  3. int *ontvangen; // global variable ontvangen (recieved)
  4.  
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7. ontvangen = p.getStartpoint(); // request the pointer
  8.  
  9. cout << ontvangen[0] << " Startpunt ontvangen" << endl; // printout value of ontvangen[0]
  10. cout << ontvangen[1] << " Startpunt ontvangen" << endl; // printout value of ontvangen[1]
  11. cout << ontvangen[2] << " Startpunt ontvangen" << endl; // printout value of ontvangen[2]
  12.  
  13. }

now the output of these are:

line 4: 25 Startpunt ontvangen
line 5: 1611638598 Startpunt ontvangen
line 6 : 1612237512 Startpunt ontvangen

my conclusion:

ontvangen[0] = 25 // like expected
ontvangen[1] = 1611638598 // not expected
ontvangen[2] = 1612237512 // not expected

why does this happen and how can i fix this ??

Thanks in advance,

Greets Ronald van Meer
Student University of applied sciences Rotterdam ( The Netherlands )
Last edited by RonaldvanMeer; Oct 21st, 2008 at 12:47 pm. Reason: adding extra info
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RonaldvanMeer is offline Offline
3 posts
since Oct 2008
Oct 21st, 2008
0

Re: C++ Array pointer problem

Well your variables below are local to the function. So when you leave the function, they don't exist and so their addresses now probably point to garbage values.

You should copy the values over onto your start pointers array, instead of pointing it to an address of a local variable.

int beginpunta = 25; // static value -> will be changed later
int beginpuntb= 5; // static value -> will be changed later
int beginpuntc = 50;// static value -> will be changed later
Last edited by stilllearning; Oct 21st, 2008 at 1:01 pm.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 21st, 2008
0

Re: C++ Array pointer problem

stilllearning,
thanks for your fast reply.

I have put these variables out side of the method, declaring them as global variables and that solved my problem.

thank you!

class: computerPlayer.cpp

cpp Syntax (Toggle Plain Text)
  1.  
  2. int *startPoint[4]; // global variable
  3.  
  4. int beginpunta = 25; // static global value -> will be changed later
  5. int beginpuntb= 5; // static global value -> will be changed later
  6. int beginpuntc = 50;// static global value -> will be changed later
  7.  
  8.  
  9. int* computerPlayer::getStartpoint(){
  10.  
  11. startPoint[0] = &beginpunta; // set array[0] with the value found on adres of beginpunta
  12. startPoint[1] = &beginpuntb; // set array[1] with the value found on adres of beginpuntb
  13. startPoint[2] = &beginpuntc; // set array[2] with the value found on adres of beginpuntc
  14.  
  15. cout << *startPoint[0] << " Pos 0" << endl; // print out value on location startPoint[0]
  16. cout << *startPoint[1] << " Pos 1" << endl; // print out value on location startPoint[1]
  17. cout << *startPoint[2] << " Pos 2" << endl; // print out value on location startPoint[2]
  18.  
  19. return *startPoint; // give the pointer to the caller of this function
  20. }
Last edited by RonaldvanMeer; Oct 21st, 2008 at 7:09 pm. Reason: Fixed the Code now it works
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RonaldvanMeer is offline Offline
3 posts
since Oct 2008

This thread is solved

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.
Message:
Previous Thread in C++ Forum Timeline: Testing if char is a number
Next Thread in C++ Forum Timeline: MySql++ Link Errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC