Is there such a thing as a generic function pointer in C that can be assigned/casted to a more restrictive prototype?

后端 未结 3 1444
名媛妹妹
名媛妹妹 2021-01-04 04:40

I have the need to dynamically link against a library at run-time and resolve a series of functions using dlsym. My first thought was to use an array of functio

3条回答
  •  被撕碎了的回忆
    2021-01-04 04:58

    You should probably define your list as void *functionList[2], since dlsym returns a void *. Once you know which function you have, you can cast it to the proper type.

    void *functionList[2];
    
    ...
    
    int (*functionA)(int) = (int(*)(int))functionList[0];
    char (*functionB)(char,int) = (char(*)(char, int))functionList[1];
    

提交回复
热议问题