I have a function called add which adds two integers as shown below:
int add(int a, int b){
return a + b;
}
and I wanted to create a pointer to my function named a and call my function using it
printf("The sum of 45, 23: %dn", a(45, 23));
I did it by creating a type for my function with typedef
typedef int (*Aptr) (int, int);
Aptr a = &add;
printf("The sum of 45, 23: %dn", a(45, 23));
The sum of 45, 23: 68
But when I tried to use the type in the typedef statement with my variable, it causes an error and I tried removing the replacement name too but it is throwing an error. If the purpose of typedef is just creating a new name for existing data type why is this not working?
int (*Aptr) (int, int); a = &add;
int (*) (int, int); a = &add;
For both cases:
[Error] 'a' undeclared (first use in this function)
[Note] each undeclared identifier is reported only once for each function it appears in
[Warning] implicit declaration of function 'a' [-Wimplicit-function-declaration]
is there any other way to state the data type of a function to function pointer?
Abenaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.