0

i got a little problem with my integer machine, si it's an abstract integer machine, from the textbook i need to make a RESET and INC (increment) button. The RESET button set the final state with 0, and INC button increment the value of current integer, so when we push the INC button 5 times, the output will be 5, here's what i got, could you guys please check ? Thanks

void RESET (int CI){
    CI = 0;
}

void INC (int CI, int Value){
    CI = Value;
    CI = Value + 1;
}
#endif
TobiSH
  • 2,833
  • 3
  • 23
  • 33
Abyan
  • 1

1 Answers1

1

You pass the variable by value, therefore its value won't be updated outside the function. You need to pass the variable by reference:

void RESET (int *CI){
    *CI = 0;
}

void INC (int *CI, int Value){
    *CI = Value + 1;
}

and call the function like so:

INC(&your_variable, 5);
Kfir Ventura
  • 262
  • 2
  • 9
  • Thank you for your help, I really appreciate it, but the output is 6 if I put 5 in it, so put printf("%d\n", Value -1); – Abyan Apr 22 '21 at 09:28