Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
136 user(s) are online (118 user(s) are browsing Forums)

Members: 1
Guests: 135

Raziel, more...

Support us!

Headlines

 
  Register To Post  

Passing a variable to function by reference
Just can't stay away
Just can't stay away


See User information
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??

Go to top
Re: Passing a variable to function by reference
Not too shy to talk
Not too shy to talk


See User information

For IDOS->Printf %d is a 16 bit integer, but the compiler puts a 32 bit integer on the parameter, so you print only the upper half of the 32 bits which is always zero for numbers below 65536. You have to use %ld to print it properly.

You should consequently use either printf or Printf everywhere, but don't mix them. You only get confused like in this case.


Go to top
Re: Passing a variable to function by reference
Just can't stay away
Just can't stay away


See User information
thanks for the answer, enlightening!

Go to top
Re: Passing a variable to function by reference
Just can't stay away
Just can't stay away


See User information
@nubechecorre

Generally I would recommend using printf() over IDOS->Printf() since the former will with newer gcc versions give you a warning if the format string and parameters don't match.

Of course you should also always compile with warnings enabled (-Wall) and ideally -Werror as well which makes gcc treat warnings as errors.

Go to top
Re: Passing a variable to function by reference
Just can't stay away
Just can't stay away


See User information
Thanks Salass00

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project