Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Please help! Programming in C code: Write a function called doubler that is passed the address of a floating-point variable and doubles the value of that original variable. Confirm that the original was doubled by printing its value before and after calling doubler.

hartnn (hartnn):

what have you tried?? you'll need a float variable : `float var = 0;` `&var` will give you the address of the memory location where this variable is stored. when the function call to 'doubler' is made, we pass this address: &var. >> This is called 'pass by reference', where we pass the memory address of the variable. `float answer = doubler(&var);` so your double function definition would look something like this: `float doubler(float* arg)` `{` ` return (*arg)*2; //` `}` the address of var, is caught in pointer arg. *arg will give the `value` pointed out by arg. (*arg)*2 will give the required result which we pass back and will be cause in the float variable 'answer' which you can print. Give it a try! ask doubts :)

hartnn (hartnn):

will be caught** in the float variable

OpenStudy (rsmith6559):

Actually, wouldn't it be: void doubler( float* arg ) { *arg *= 2; }

OpenStudy (anonymous):

@rsmith6559 is correct.

hartnn (hartnn):

oh yeah, we've passed by reference, so we also don't need to catch anything

jaynator495 (jaynator495):

Hartnn.. you... you code? *mouth drops*

hartnn (hartnn):

LOL Jay, I code for living :P

jaynator495 (jaynator495):

WHY DIDNT I KNOW THIS! XD

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!