Why the return type is void* ,why not int because the function square is returning result which is of int type ? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

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

4th Jan 2019, 3:33 AM
Ganesh Moota
Ganesh Moota - avatar
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 .
4th Jan 2019, 9:06 AM
Ganesh Moota
Ganesh Moota - avatar
- 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); }
4th Jan 2019, 8:42 AM
CloudWay
CloudWay - avatar
- 1
I replaced all voids with int and now its working
4th Jan 2019, 8:43 AM
CloudWay
CloudWay - avatar
- 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
4th Jan 2019, 9:09 AM
CloudWay
CloudWay - avatar
- 1
But I'm not shure
4th Jan 2019, 9:09 AM
CloudWay
CloudWay - avatar