r/cprogramming • u/north9172 • Jul 31 '26
casting a void function pointer as a int fp
(solved)
Hello,
Today I tried making an array of function pointers.
My first prototype was doing:
int (*fptr[2])(int, int)But what if I wanted to store a function with different parameters and return value?
I tried:
void (*fptr[2])()And then later type casting the function I wanted to store:
fptr[1] = add;printf("%d", ( int (int, int) ) fptr1);
But apparently it's not valid:
used type 'int (int, int)' where arithmetic or pointer type is requiredIs it possible to cast the void function pointer as a int fp with parameters? Thanks.
4
Upvotes
9
u/TheFlamingLemon Jul 31 '26
This doesn’t exactly answer your question but usually in this sort of situation the function parameter will just be a void pointer and each function is responsible for knowing what to cast that pointer into to retrieve its parameters
5
u/flyingron Jul 31 '26
Note that there's really not such a thing as a void function or int function. It is a function RETURNING one of those types.
int (int, int) is the declaration of a function ***NOT*** a pointer. Where as function names convert to pointers to them, the function and its pointer are distinct types.
The pointer type is int (*)(int, int)
Further, nothing says you can pass any sort of function pointer to an argument expecting an int (%d). Even if you'd tried %p, that still would be wrong as it wants an object pointer. Nothing says function pointers and object pointers are the same size (they're not on some older platforms).
2
u/tstanisl Jul 31 '26
Yes. Just do:
I suggest using a typedef for desired function type to make things a bit more "human parsable":
2
u/pjl1967 Jul 31 '26
For these kinds of questions, I recommend using
cdecl, e.g.:That is, you type what you want in pseudo-English and
cdeclwill print what the corresponding C (aka, gibberish) is.FYI,
cdeclalso goes the other way:That aside, if you want to have an array of different types of pointers, either have an array of
unionof them; or have the array ofuintptr_t.2
u/sciencekm Jul 31 '26
All function pointers are of the same size, and so, yes, you can cast them among each other.
2
u/duane11583 Jul 31 '26
Normally I hate typedefs with a passion I believe typedefs are evil
But in this case they are very helpful because thy kind of act like ()s for types
First make a type for the function not a function pointer there is no star here
typedef int myfunctype( int, int);
Second optional create a type for a func pointer
typedef myfuncptr *myfunctype;
Create an array of 10 func pointers
extern myfuncptr thearray[10];
2
u/SheikHunt Aug 01 '26
Other comments have already answered, but I wrote the paragraphs before I realized that, so I'm just sending it:
In C, there really isn't a feasible way to store "Function with any number of parameters that returns either something or void". At least, not in the way other languages do/might
I know of one example you can follow, namely the pattern that the pthread_create() function follows: it takes a function pointer as one of its arguments. The relevant argument is of type:
void * (*)(void *)
That is to say, "a function pointer to a function that takes a void pointer and returns a void pointer".
In that case, it's your job as the programmer to know the correct void pointer to pass in, and how to use the returned void pointer. Luckily, that's not particularly difficult to know, because chances are you wrote the function that you're calling. The given function could also just be something that turns the void * into usable arguments and calls the intended function with the correct arg(s).
Generally speaking, in C, this isn't doable.
1
u/DawnOnTheEdge Aug 04 '26
To get language-lawyery, function pointers and object pointers do not have to be the same size, and a conforming implementation doesn’t have to let you cast one to the other.
However, all mainstream operating systems let you cast a function pointer to a
void*and back, like when looking up a symbol in a dynamic library.Therefore, what will work on all but a few unusual systems is:
Note that the
%dformat specifier is asigned int, which is not wide enough to hold a pointer on most systems.1
u/acadia11x Aug 01 '26 edited Aug 01 '26
Yes, and there is an even easier way to get your question answered.
0
u/picturesfromthesky Jul 31 '26
Read about variadic functions.