So i'm writing a snake game using arrays, and i have to change the values of the row and column, not create copies, therefore i'm using pointers.
int main(){
int row=0,column=0;
int *rowPtr;
rowPtr=&row;
int *colPtr;
colPtr=&column;
I need to change these variable's value, and also check if they're at the place i want. I have a function to create my snake on the gameboard, prototype was created too.
create_snake(&row,&column,row,column,2); //creating snake
the creating snake function is here, there are parameters for row and column also, so that i can use them on the array and do a checkup, but i also used pointers, so that i'll change row's and column's values before using the actual row and column
void create_snake(int *rowPtr,int *colPtr,int row,int column,int x){
pointerset(rowPtr,colPtr);
printf("%d %d",row,column);
if(board[row][column]==1){
create_snake(rowPtr,colPtr,row,column,x);
}else if(board[row][column]==0){
board[row][column]=x;
}else{
create_snake(rowPtr,colPtr,row,column,x);
}
}
To give random numbers for row and column, there was another function.
void pointerset(int *rowPtr,int *colPtr){
*rowPtr=rand()%;
*colPtr=rand()d;
}
I've tried to see where the variable's value's change, therefore i used a
printf("%d %d",row,column);
When i use it in the function create_snake()
it gives me 0 0 for row and column, when i use this printf under the function create_snake()
in main, after function works and stops, it gives me random numbers i've created, rather than 0 0 but i need them to change when the function works. I simply couldn't solve where i failed using pointers.
I've created the function pointerset()
because if i was simply writing
*rowPtr=rand()%;
*colPtr=rand()d;
to the function create_snake()
it wasn't working.
CodePudding user response:
So here is the thing, *rowPtr gets the integer value of the pointer, while rowPTr doesnt( i guess gives memory adress? im not sure)
i removed the arguments in the function called row
and column
but instead used pointer for everything.
I thought i couldn't use pointers to check my board which is an array for their index, but i can simply type board[*rowPtr][*colPtr]
and it works, i was writing without asteriks.