this is my code:
#include <stdio.h>
int getsum(int a,int b){
return a+b;
}
void fun1(int a,int b,int (*c)(int a,int b)){//Standard writing method
int sum=getsum(a,b);
printf("%d\n",sum);
}
void fun2(int a,int b,int c(int a,int b)){//The function itself serves as a parameter
int sum=getsum(a,b);
printf("%d\n",sum);
}
int main(){
int a=1;
int b=2;
fun1(a,b,getsum);
fun2(a,b,getsum);
return 0;
}
Both of these functions yield the same result after running The results are all equal to 3 The c parameter of the first function is written in the standard way, while the second one directly represents the function itself
I just want to know if both of these writing methods are correct
From the C Standard (6.7.6.3 Function declarators (including prototypes) Constraints)
Thus these two function declarations
declare the same one function. For the first declaration the compiler itself adjusts the parameter having the function type to a parameter having a pointer type to the function type.
In function declarations that are not at the same time its definition you may ommit a parameter name. So the above declarations you may also write like
It seems in bodies of your functions you mean
instead of
Pay attention to that such an adjustment of a function specifiers does not occur outside function parameter lists.
Consider the following demonstration program.
#include <stdio.h>
In this declaration
the typedef name
Fnis not adjusted to a pointer type. Here is declared the functiongetsum.