+ 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; }

6th May 2020, 11:20 PM
SAzidsukc
SAzidsukc - avatar
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*).
6th May 2020, 11:23 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
0
Thanks 😊 now it is cleared completely
7th May 2020, 9:35 AM
SAzidsukc
SAzidsukc - avatar