0

How can I cast void* to int ( * () ) (int,...)?

The void* is coming from a dlsym. This code isn't compiling:

typedef  int ( *PSYS () ) (int,...);
PSYS getf =  (PSYS) dlsym(lib, "function" );
  • why do you wanna cast `void *` to other type? – haccks Jan 04 '14 at 02:50
  • @haccks Because a `void*` is what is returned by `dlsym`. – Pascal Cuoq Jan 04 '14 at 02:51
  • @PascalCuoq; Should't it converted to that type implicitly? (Don't know much about this). – haccks Jan 04 '14 at 02:52
  • [tag:c] or [tag:c++]? The answer is different for different languages. – johnsyweb Jan 04 '14 at 02:53
  • 2
    @haccks 1- The conversion from void* to function pointer is undefined in standard C, so it is not going to happen implicitly. 2- Even conversions from function pointer to function pointer do not happen implicitly on application (they happen on assignment of a function pointer to a function pointer lvalue, I think). – Pascal Cuoq Jan 04 '14 at 02:54
  • 3
    "isn't compiling" is not a known error message. Tell us the one you're actually getting. it probably has to do with the syntax of the type in your typedef, which looks nonsensical. – Jim Balter Jan 04 '14 at 02:55
  • ; ( C++ ) main.cpp|25|error: invalid cast to function type ‘PSYS {aka int (*())(int, ...)}’| function ‘int (* getpsys())(int, ...)’ is initialized like a variable| –  Jan 04 '14 at 02:56
  • What are the parens after PSYS supposed to represent? Not the arguments ... those are `(int,...)`. – Jim Balter Jan 04 '14 at 02:59
  • the function looks like this int ( *function() ) (int,...){ return &systrig;} –  Jan 04 '14 at 03:00
  • See http://stackoverflow.com/q/1096341/951890 – Vaughn Cato Jan 04 '14 at 03:01
  • Ok, I think you're trying to cast to a function, rather than a pointer to a function. Try using `PSYS*` instead of `PSYS`. ... See my answer. – Jim Balter Jan 04 '14 at 03:08

2 Answers2

1

If the symbol is a function pointer your typedef may be wrong. Should be:

typedef int (*PSYS)(int, ...);
TimDave
  • 652
  • 3
  • 6
  • no, actually it is a function pointer returning a function pointer –  Jan 04 '14 at 02:59
  • So the symbol you are trying to get out is a function pointer returning a function pointer? If so, that's wild. So is it a utility function to get other functions in the library? – TimDave Jan 04 '14 at 03:02
  • True, the other function is inline and can't be exported, but maybe there are other solutions. –  Jan 04 '14 at 03:05
  • Does this help? http://stackoverflow.com/questions/10758811/c-syntax-for-functions-returning-function-pointers – TimDave Jan 04 '14 at 03:10
1

PSYS is the type of a function, not a pointer to a function. You want

typedef  int ( *PSYS () ) (int,...);
PSYS* getf =  (PSYS*) dlsym(lib, "function" );
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • Geat, compil works, i'm just afraid about segfaults now ^^. Thank you –  Jan 04 '14 at 03:14
  • @Ododo Presumably it doesn't segfault on that line, but somewhere else. Good luck with your debugging. :-) – Jim Balter Jan 04 '14 at 03:23