+ 3

Why I'm not able to get output in float, But it's working for int.

#include<stdio.h> void* fun (const void* num); int main(){ float sqrts; float x = 5.2; sqrts = fun(&x); printf ("%f square is %f", x, sqrts); } void* fun (const void* num){ float result; result = (*(float*)num) * (*(float*)num); return result; }

30th Jun 2018, 7:28 AM
Marana Coder
Marana Coder - avatar
3 Respuestas
+ 1
Now, it works for float also :) EDIT: @ lambor gopi, in your sixth line, sqrts = fun(&x); First thing, x is a float, so fun's parameter and return type should also be float. Secondly, result = (*(float*)num) * (*(float*)num); type casting isn't required here, as *num is already a float type. https://code.sololearn.com/cAftQ4HYwonp/?ref=app
30th Jun 2018, 7:50 AM
777
777 - avatar
0
Can u pls eloborate the reason why my program working for int and not working for float this doubt araises while reading "functions using void pointers" in sololearn c
30th Jun 2018, 7:54 AM
Marana Coder
Marana Coder - avatar
0
ok tnq. can u create one program like this using void pointer functions
30th Jun 2018, 8:30 AM
Marana Coder
Marana Coder - avatar