This looks way too stupid to be posted in beginners too :-/
This is my 1st post so pardon me for any mistake i make in creating posts :)
I wanted to create a double pointer so that i can hold a 2d array in game.
so i did this in the class declaration (header part)
1 2 3 4 5 6 7
|
class AIPathFinding{
.
.
int **clearanceValues;
.
.
}
|
and in implementation
1 2 3 4 5 6 7 8 9
|
// in constructor
clearanceValues = new int*[width + 1];
for( int i = 0 ; i < width ; i++ ){
clearanceValues[i] = new int[height + 1];
clearanceValues[i][height] = NULL;
}
clearanceValues[width] = NULL;
|
This array depends on the map size so it has to be dynamic. I read some where that there is nothing like .length for an array to get the length of the array unlike in java. The only way is to put a NULL at the end of array and iterate through the array till you hit NULL. So initialized the array with length + 1 and initialized the final variable in array to NULL.
Now while generating clearance values for path finding i needed to increment the values in the array if certain condition is met.
1 2 3 4 5 6 7 8 9 10
|
for(int y = 0 ; y < height ; y++){
for(int x = 0 ; x < width ; x++){
clearanceValues[x][y] = 1;
while( !breakWhile ){
if(ifCondition){
clearanceValues[x][y]++;
}
}
}
}
|
but when i print the clearanceValues all the values are 2 (where ever ifCondition is true, you can see that it is in while so the clearanceValues[x][y] should be equal to number of times whileCondition && ifCondition is true).
I kept break point at line
clearanceValues[x][y]++ to check how many times its is being hit. This line is being hit 4 times when x = 0, y = 0.
clearanceValues[x][y]++ is being called 4 times still its value is 2. Everytime checkpoint hits i check the value it says 2 only.
if i replace
clearanceValues[x][y]++ with
clearanceValues[x][y] = 4 then all values are becoming 4.
so i replaced
clearanceValues[x][y]++ with
clearanceValues[x][y] = clearanceValues[x][y] + 1 and also
int tmp = clearanceValues[x][y] + 1;clearanceValues[x][y] = tmp; both gave same result.
no matter how many times this line is being hit it still says 2.
From this what i understood is if i assign numeric values directly (like clearanceValues[x][y] = 5 they are being assigned perfectly but when i try doing it with variables or uniary operators they are not being assigned)
I'm a java guy not much familiar with pointers and all. I know i'm missing something small like '*' or '&' or something. please help me understand the problem i'm facing here.