+ 2
Everything is understandable but why did I use return (& result) instead of return result? Pls answer!!!
#include <stdio.h> void* square (const void* num); int main() { int x; int * sq_int; x = 6; sq_int = (int *) square(&x); printf("%d squared is %d\n", x, *sq_int); return 0; } void* square (const void *num) { static int result; result = (*(int *)num) * (*(int *)num); return &result; }
2 Antworten
+ 4
Because the return value of the square function is a void pointer.
When the function is called it returns the address of static int result using the address operator "&" and assigns it to sq_int (which is a pointer to integer) casting it with (int*).
0
Thanks 😊 now it is cleared completely