Is pointer arithmetic not allowed? Or calling main()?
Is pointer arithmetic not allowed? Or calling main()?
Tiny C (from 2006, 5 years old) gives:
function pointer expected
MS C 6 (from 1998, 13 years old) gives error C2296: '-' : illegal, left operand has type 'void (__cdecl *)(int )'
Turbo C 2.01 (from 1988, 23 years old) gives: Size of structure or array not known in function
etc.I think the really clean solution (as far as I know, at least it works with all the compilers I've mentioned) is:
If you do want to put a (non-function) pointer into an integer, convert to (C99) [u]intptr_t. Of course, that does not have to be defined...
A program I'm writing now uses void* as a generic function pointer, which I cast to different function signatures as required depending on usage. I'm guessing that's considered harmful, is there a better or more idiomatic way to do this?
Architectures where code pointers are bigger than data pointers are fairly exotic, I think. The AS/400 might be an example, I'm not sure. However! You can avoid that particular problem by using (void* )(void) as your generic function pointer type, rather than simply void *.
That said, this is not that likely to be a problem in practice on Unix machines (Windows has more than one calling convention - I have no idea how much of a problem that is.)
So, for an array of function pointer that would be legal behaviour, but for pointers to plain functions, it is undefined behaviour.