C++ Array pointer problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 2
Reputation: RonaldvanMeer is an unknown quantity at this point 
Solved Threads: 0
RonaldvanMeer RonaldvanMeer is offline Offline
Newbie Poster

C++ Array pointer problem

 
0
  #1
Oct 21st, 2008
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()

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: C++ Array pointer problem

 
0
  #2
Oct 21st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: RonaldvanMeer is an unknown quantity at this point 
Solved Threads: 0
RonaldvanMeer RonaldvanMeer is offline Offline
Newbie Poster

Re: C++ Array pointer problem

 
0
  #3
Oct 21st, 2008
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

  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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC