I need to typedef a function pointer type to create an array of pointers, and to declare a large number of functions that will end up in the array. However, I can't find a way to do both of these things at the same time: either I can get pointers for the array, or I can declare functions, but not both.
Any ideas on how I can get this code to work without lots of pointless text, or am I stuck with repeating the entire function signature for every function I need to declare? Thanks.
#include <stdio.h>
// declare a fn ptr type named 'myfunc'
typedef void (*myfunc)(int);
//myfunc A,B,C,etc; **doesn't work, because of the (*)
void A(int); // does work, but becomes very tedious
int main() {
myfunc B = A; // actually assigning a large 2D array
A(42);
B(43);
}
void A(int foo) {
printf("%d\n", foo);
}
In C++ you may use
std::remove_pointer_t<myfunc> A, B, C;
Or declare one typedef for the function type and another for the pointer to function type:
typedef void myfunc(int);
typedef myfunc * myfuncptr;
myfunc A, B, C;
The latter works in C and C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With