I try to pass a variable by reference to a function and change its value. Inside that function it works but when I print the value of the variable outside the function the value is different.. I'd like to understand what I am doing wrong as I need it for my program..
I declare this variable as global plus the definition of two functions outside (before) the main function:
int a; void change_sound_1(int *); void change_sound_2(int *); .....
inside the main function i have this code:
switch(code) { case 0: IDOS->Printf("Sound rate is: 11025\n"); change_sound_1(&sound_code); IDOS->Printf("sound_code is:%d\n", sound_code); break; case 1: IDOS->Printf("Sound rate is: 22050\n"); change_sound_2(&sound_code); IDOS->Printf("sound_code is:%d\n", sound_code); break; } break;
These are the two functions that I use to modify the int variable:
void change_sound_1(int *a){ *a=0; printf("sound code is:%d\n", *a); *a=*a+1; printf("sound code is:%d\n", *a); } void change_sound_2(int *b){ *b=0; printf("sound code is:%d\n", *b); *b=*b+2; printf("sound code is:%d\n", *b); }
But when i print the variable, for the second time, in the main function, it prints 0 (zero).. Shouldn't it print the the same value that the two functions print? as I have modified its value using the pointer??
cross-post: http://www.amigans.net/modules/xforum/viewtopic.php?forum=25&topic_id=7822&order=
Yes Thomas, i posted it both here and there :-)
Thanks to all