| | |
C++ Array pointer problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
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()
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:
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 )
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)
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:
cpp Syntax (Toggle Plain Text)
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 )
Last edited by RonaldvanMeer; Oct 21st, 2008 at 12:47 pm. Reason: adding extra info
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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.
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.
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
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
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)
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 }
Last edited by RonaldvanMeer; Oct 21st, 2008 at 7:09 pm. Reason: Fixed the Code now it works
![]() |
Similar Threads
- C++: compile error "subscripted value is neither array nor pointer" (C++)
- file pointer problem (C)
- Help with code (C)
- C++ second largest problem (C++)
- Problem with a snippet of code in a shell program (C)
- dynamic array of structures problem (C++)
- 'C' initializer problem (C)
- pointer problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Testing if char is a number
- Next Thread: MySql++ Link Errors
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





