Why does this variable change after the return statement?

I have a function with a signature:

int exe(int stack[][STACKSIZE], int sp[], int reg[][REGISTERSIZE], int next_instruct[],
        int next_inst[], int cur_proc, int *terminate);

It has the last two lines:

printf("TWO cur_proc: %d\n",cur_proc);
return NORMAL;

And called like this:

printf("ONE cur_proc: %d\n",cur_proc);
msg = exe(stack,sp,reg, next_instruct, next_instruct, cur_proc, &terminate);
printf("THREE cur_proc: %d\n",cur_proc);

And I pass cur_proc, which is considered a read-only variable (no matter what it should be passed by value) inside exe(). Do my things inside exe().

And my conclusion:

ONE cur_proc: 1
TWO cur_proc: 1
THREE cur_proc: -1

This is very confusing to me, because I see no reason that it could be rewritten in negative.

What is the possible reason for this strange behavior?

+5
source share
2 answers

You are probably writing outside of one of the arrays.

, . printf() , .

, , , - , . const-, , , -, , .

, cur_proc , cur_proc - - .

+4

exe , , , ( ).

, , cur_proc - , .

" ". cur_proc , , .

+3

All Articles