Why my code doesn't work... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why my code doesn't work...

//I don't understand why my compiler (Code::Blocks) gives me a warning when I execute the following code: #include <stdio.h> void *square(const void *num); int main() { int a = 5; int result; result = square(&a); printf("%d squared is %d", a, result); return 0; } void *square(const void *num) { int r; r = (*(int*)num)*(*(int*)num); return r; } //Can anybody explain why? When I change from return r; to return (int*)r; in the 'square' function and from square(&a) to (int)square(&a) in the 'main' function, it works. Also, should I declare the variable 'r' in the 'square' function to be static because it is being passed out of the function (local variable)? Thanks.

6th Jan 2019, 7:20 AM
Konybud
Konybud - avatar
1 Answer
+ 1
When you said I implicitly converting from pointer to int, and vice versa. Do you mean the variable 'r' in the 'square' function? If so, I need to typecast the variable 'r' to be return (int*)r so the function will know I'm returning an integer value? Also, when we type casting the void pointer why do we put an asterisk inside the bracket? I'm really confusing...
6th Jan 2019, 11:07 AM
Konybud
Konybud - avatar