+ 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. }
7 Réponses
+ 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.
+ 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...
+ 3
Ok I began to understand a bit more... thanks :)
+ 2
Why do you use void* in the first place?
+ 2
Because I followed the tutorial.
+ 1
My question is still not answered...
0
Maybe you should use static int r for the declaration of r variables.