I'm trying to write a function that changes screen in my simple C snake game.
main(){
int stage = 0;
...
..
.
while(stage!=0){
//if snake hits wall
changeStage(stage);
}
}
function:
void changeStage(int stage){
stage = 1;
}
This code does not update the code, it will keep on running. What is wrong with my code?
stage is passed by value to changeStage. stage = 1 only changes the local value of stage in changeStage, not the value of stage in main. You have to pass a pointer instead:
while (stage != 0) {
changeStage(&stage);
}
void changeStage(int *stage) {
*stage = 1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With