Why my compiler gives me warnings? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why my compiler gives me warnings?

#include <stdio.h> void *square(const void *num); int main() { int a = 5; int result; result = square(&a); /*Assignment makes integer from pointer without a cast.*/ printf("%d squared is %d", a, result); return 0; } void *square(const void *num) { int r; r = (*(int*)num)*(*(int*)num); return r; //return makes pointer from integer without a cast. }

18th Apr 2019, 8:20 AM
Konybud
Konybud - avatar
7 Answers
+ 3
You declare the function as void* and it means that the function does not expect a return statement within itself. Make it an int function. Also make it's parameter int or const int.
18th Apr 2019, 12:09 PM
Alexander Velinov
Alexander Velinov - avatar
+ 3
I already did, but it still gives me warnings... According to the comments on "Functions using void pointers", they said to do: -> return (int*) r; -> (int)square(&a); I did the above and it works, but why doesn't Sololearn tell me that in the tutorial? Also, why doesn't it be like this: return (int)r. I thought (int*) is for pointer, isn't it? Please answer...
18th Apr 2019, 2:07 PM
Konybud
Konybud - avatar
+ 3
Ok I began to understand a bit more... thanks :)
18th Apr 2019, 5:04 PM
Konybud
Konybud - avatar
+ 2
Why do you use void* in the first place?
18th Apr 2019, 2:24 PM
Alexander Velinov
Alexander Velinov - avatar
+ 2
Because I followed the tutorial.
18th Apr 2019, 3:04 PM
Konybud
Konybud - avatar
+ 1
My question is still not answered...
18th Apr 2019, 3:42 PM
Konybud
Konybud - avatar
0
Maybe you should use static int r for the declaration of r variables.
4th May 2020, 7:50 AM
Samuel Mathew
Samuel Mathew - avatar