Using SWIG with pointer to function in C struct

前端 未结 2 369
长发绾君心
长发绾君心 2020-12-21 10:35

I\'m trying to write a SWIG wrapper for a C library that uses pointers to functions in its structs. I can\'t figure out how to handle structs that contain function pointers

2条回答
  •  执念已碎
    2020-12-21 11:23

    You forget to "return t;" in init_test():

    #include  
    #include  
    
    typedef struct {
     int (*my_func)(int);
    } test_struct;
    
    int add1(int n) { return n+1; }
    
    test_struct *init_test(){
      test_struct *t = (test_struct*) malloc(sizeof(test_struct));
      t->my_func = add1;
      return t;
    }
    
    int main(){
      test_struct *s=init_test();
    
      printf( "%i\n", s->my_func(1) );
    }
    

提交回复
热议问题