return type as (void *) but returning interger value. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

return type as (void *) but returning interger value.

Suppose i have a function with fun declaration as: void * foo(int a, int b); But the function definition is as below: //******Function Definition*********// void * foo(int A, int B) { int SUM; SUM = A +B; return SUM; } I call this function in main as below: //******main function***********// int main(void) { int a=10,b=4; int sum; sum = foo(a,b); printf("sum=%d\n",sum); } ///////**********Questions: 1) What will return from the function and what will be the value in "sum" variable in main function? 2) What will be the result if the datatype of "sum" variable is changed to (int*) or (char *) or (char) or anything else? 3) What will be the behavior if i return (&SUM) from "foo" function?

11th May 2017, 2:22 PM
Pranjal Upadhyay
Pranjal Upadhyay - avatar
2 Answers
+ 3
Error. Invalid convertion from int to void*. Cannot compile.
11th May 2017, 3:35 PM
Rrestoring faith
Rrestoring faith - avatar
0
hi @Rrestoring faith , I tried the code its working... please find the working program below: //code............... #include <stdio.h> void * foo(int A, int B) { int SUM; SUM = A +B; return SUM; } int main(void) { int a=10,b=4; int sum; sum = foo(a,b); printf("sum=%d\n",sum); }
11th May 2017, 5:05 PM
Pranjal Upadhyay
Pranjal Upadhyay - avatar