Background
I wrote a function named removeIllegalMoveFromMovesCell
to remove data from some variables, let's call it x and y for now.
The value of x does change inside the function but will not update outside of the function.
Calling the method:
removeIllegalMoveFromMovesCell(movesArr[row][col].moves,&(movesArr[row][col].size), row, col);
Explaining the variables
x and y variables that was mentioned above are movesArr[row][col].moves
and movesArr[row][col].size
, they are based on those structs:
typedef struct _move {
char rows;
char cols;
} move;
typedef struct _movesArray {
unsigned int size;
move* moves;
} movesArray;
And here is how I create them:
movesArray* arr = (movesArray*)malloc(sizeof(movesArray));
move* movesArr1 = (move*)malloc(2 * sizeof(move));
move* movesArr2 = (move*)malloc(2 * sizeof(move));
move move1, move2, move3, move4;
move1.rows = -2;
move1.cols = 3;
move2.rows = 1;
move2.cols = 3;
move3.rows = -2;
move3.cols = 5;
move4.rows = 1;
move4.cols = 2;
movesArr1[0] = move1;
movesArr1[1] = move2;
movesArr2[0] = move3;
movesArr2[1] = move4;
arr[0].size = 2;
arr[1].size = 2;
arr[0].moves = movesArr1;
arr[1].moves = movesArr2;
removeIllegalMoveFromMovesCell
method:
void removeIllegalMoveFromMovesCell(move* moves, int* size, int currRow, int currCol) {
int i;
int cloneIndex = 0;
move* movesClone = (move*)malloc((*size - 1) * sizeof(move));
for (i = 0; i < *size; i++) {
if (!isMoveOutOfBounds(currRow, currCol,
getPureNumber(moves[i].rows), getPureNumber(moves[i].cols))) {
movesClone[cloneIndex].rows = moves[i].rows;
movesClone[cloneIndex].cols = moves[i].cols;
cloneIndex++;
}
}
moves = (move*)realloc(moves, cloneIndex * sizeof(move));
for (i = 0; i < cloneIndex; i++) {
moves[i] = movesClone[i];
}
*size = cloneIndex;
}
The Problem and what I have tried so far:
While size
variable changes outside of the method (And by that changing movesArr[row][col].size
), moves
variable receive all of movesClone
variable inside the method and will change its value inside the method but won't change outside of it (movesArr[row][col].moves
).
move
is acting as a local variable although I passed his reference and not its value, I cannot understand why it won't change outside of the method
Extra info:
Here is getPureNumber
function that is used inside removeIllegalMoveFromMovesCell
int getPureNumber(char c) {//simple return `movesArr[row][col].size` as number
if (c < 0)
return c - 0;
return c;
}