Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren0
Why the return type is void* ,why not int because the function square is returning result which is of int type ?
#include <stdio.h> void * square (const void *num); int main() { int x, sq_int; x = 6; sq_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); }
5 Antworten
0
It works fine with void* too but what I wanted to know is that the function is returning the result which is of type int then why should the return type be pointer in the above case .
- 1
#include <stdio.h>
int * square (const int *num);
int main() {
int x, sq_int;
x = 6;
sq_int = square(&x);
printf("%d squared is %d\n", x, sq_int);
return 0;
}
int* square (const int *num) {
static int result;
result = (*(int *)num) * (*(int *)num);
return(result);
}
- 1
I replaced all voids with int and now its working
- 1
I'm actually not into C but I worked with Java some time ago and I'd say that its because the argumend gets defined as void so he works with void in the code and returns void
- 1
But I'm not shure