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()

int *startPoint[4]; // global variable

int* computerPlayer::getStartpoint(){
	
	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

        startPoint[0] = &beginpunta;     // set array[0] with the value found on adres of beginpunta
	startPoint[1] = &beginpuntb;     // set array[0] with the value found on adres of beginpuntb
	startPoint[2] = &beginpuntc;     // set array[0] with the value found on adres of beginpuntc 

	cout << *startPoint[0] << " Pos 0" << endl; // print out value on location startPoint[0]
	cout << *startPoint[1] << " Pos 1" << endl; // print out value on location startPoint[1]
	cout << *startPoint[2] << " Pos 2" << endl; // print out value on location startPoint[2]

	return *startPoint;  // give the pointer to the caller of this function
}

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:

computerPlayer p(3,3,3); // creation of computerplayer
int *ontvangen;  // global variable ontvangen (recieved)

int _tmain(int argc, _TCHAR* argv[])
{
        ontvangen = p.getStartpoint(); // request the pointer

	cout << ontvangen[0] << " Startpunt ontvangen" << endl; // printout value of ontvangen[0]
	cout << ontvangen[1] << " Startpunt ontvangen" << endl; // printout value of ontvangen[1]
	cout << ontvangen[2] << " Startpunt ontvangen" << endl; // printout value of ontvangen[2]

}

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 )

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

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

int *startPoint[4]; // global variable

      int beginpunta = 25; // static global value -> will be changed later
      int beginpuntb= 5; // static global value -> will be changed later
      int beginpuntc = 50;// static global value -> will be changed later


      int* computerPlayer::getStartpoint(){

      startPoint[0] = &beginpunta; // set array[0] with the value found on adres of beginpunta
      startPoint[1] = &beginpuntb; // set array[1] with the value found on adres of beginpuntb
      startPoint[2] = &beginpuntc; // set array[2] with the value found on adres of beginpuntc

      cout << *startPoint[0] << " Pos 0" << endl; // print out value on location startPoint[0]
      cout << *startPoint[1] << " Pos 1" << endl; // print out value on location startPoint[1]
      cout << *startPoint[2] << " Pos 2" << endl; // print out value on location startPoint[2]

      return *startPoint; // give the pointer to the caller of this function
      }
This question has already been answered. Start a new discussion instead.