RicardasSim

func pointers

Jun 11th, 2019
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. /*
  2.  
  3. build shared library:
  4.  
  5. gcc -fPIC -c testas.c
  6. ld -shared -soname libtestas.so.1 -o libtestas.so.1.0 -lc testas.o
  7.  
  8. testas.c:
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdbool.h>
  13.  
  14. void _init()
  15. {
  16.     printf("init\n");
  17. }
  18.  
  19. void _fini()
  20. {
  21.     printf("fini\n");
  22. }
  23.  
  24. void fncG()
  25. {
  26.    printf("test 2\n");
  27. }
  28.  
  29. */
  30.  
  31. //main.c:
  32.  
  33. //-std=c99 -Wall -Wextra -Wpedantic -Wshadow
  34. //linking: -ldl -Wl,-rpath=.
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <stdbool.h>
  39. #include <string.h>
  40. #include <dlfcn.h>
  41.  
  42. void fncA()
  43. {
  44.    printf("test\n");
  45. }
  46.  
  47. int fncB(int x, int y)
  48. {
  49.     return x + y;
  50. }
  51.  
  52. void *fncC(void *p)
  53. {
  54.  
  55.    return p;
  56. }
  57.  
  58. int fnc(int x)
  59. {
  60.    return x+1;
  61. }
  62.  
  63. char fncc(char c)
  64. {
  65.    return c+1;
  66. }
  67.  
  68. //typedef char (*PFC)(); warning: return from incompatible pointer type
  69. typedef char (*PFC)(char);
  70.  
  71. char testF1(char a)
  72. {
  73.    return a+1;
  74. }
  75.  
  76. char testF2(char a)
  77. {
  78.    return a+2;
  79. }
  80.  
  81. PFC pfAddr(bool b)
  82. {  
  83.    if (b) return &testF1;
  84.    return &testF2;
  85. }
  86.  
  87.  
  88. int main (int argc, char **argv)
  89. {
  90.  
  91.    int result;
  92.  
  93.    //notes:
  94.    //the unary operator & is optional
  95.    //to make code more portable, you should always use &
  96.    //when assigning an address of a function to a function pointer
  97.  
  98.    void(*pf_fncA)()=&fncA; //=fncA;
  99.    int(*pf_fncB)()=&fncB;  //=fncB;
  100.    void *(*pf_fncC)();
  101.  
  102.  
  103.    (*pf_fncA)();
  104.  
  105.    pf_fncA();
  106.  
  107.  
  108.    result = (*pf_fncB)(1,2);
  109.    printf("%d\n",result);
  110.  
  111.  
  112.    pf_fncC = &fncC; //=fncC;
  113.  
  114.    printf("%p\n",(void*)&result);
  115.  
  116.    void *pResult = pf_fncC(&result);
  117.  
  118.    printf("%p\n",pResult);
  119.  
  120.    int (*fp)(int);
  121.  
  122.    fp = fnc;
  123.  
  124.    printf("%d\n",fp(1));
  125.    printf("%d\n",(*fp)(1));
  126.  
  127.    char (*fpc)(char);
  128.  
  129.    //fpc = fncc;
  130.    fpc = &fncc;
  131.    
  132.    printf("%c\n",fpc('a'));
  133.    printf("%c\n",(*fpc)('b'));
  134.  
  135.  
  136.    PFC pFTest;
  137.  
  138.    pFTest = pfAddr(true);
  139.  
  140.    printf("%c\n",pFTest('a'));
  141.  
  142.    pFTest = pfAddr(false);
  143.  
  144.    printf("%c\n",pFTest('a'));
  145.  
  146.    char *err;
  147.    void *lib = dlopen("libtestas.so.1.0", RTLD_NOW);
  148.  
  149.    if (!lib)
  150.    {
  151.       printf("%s\n", dlerror());
  152.       return 1;
  153.    }
  154.  
  155.    dlerror();
  156.  
  157.    printf("INFO: load lib OK.\n");
  158.  
  159.    //warning: ISO C forbids initialization between function pointer and ‘void *’
  160.    //void(*pfVoid)() = dlsym(lib, "fncG");
  161.  
  162.    void(*pfVoid)();
  163.  
  164.    *(void **)(&pfVoid) = dlsym(lib, "fncG");
  165.  
  166.    if ( (err = dlerror()) != NULL)
  167.    {
  168.       printf("%s\n", err);
  169.       dlclose(lib);
  170.       return 1;
  171.    }
  172.  
  173.    //function must be called before dlclose(lib), or there will be a segfault
  174.    pfVoid();
  175.  
  176.    dlclose(lib);
  177.  
  178.    printf("INFO: exit OK.\n");
  179.  
  180.    return 0;
  181. }
  182.  
  183. /*
  184. output:
  185.  
  186. test
  187. test
  188. 3
  189. 0x7ffe780bbff4
  190. 0x7ffe780bbff4
  191. 2
  192. 2
  193. b
  194. c
  195. b
  196. c
  197. init
  198. INFO: load lib OK.
  199. test 2
  200. fini
  201. INFO: exit OK.
  202.  
  203.  
  204. */
Advertisement
Add Comment
Please, Sign In to add comment