Accessing an array of pointers within a structure from Java with SWIG

送分小仙女□ 提交于 2019-12-05 07:00:50
Svetoslav Marinov

The solution is very easy. Just use in a swig interface:

%include <carrays.i>
%array_functions(Boo *, boo_array);

And then access from java with:

SWIGTYPE_p_p_Boo results = foo.getData();
for(int i = 0; i < foo.getSize(); i++) {
    Boo booResult = foo.boo_array_getitem(results, i);
}

to retrieve the content of the array.

you can always do a malloc, example for 1d tab would be :

int main (void)                                                          
 {                                                                
    int   size;                                                        
    Foo a;

  size = 2;
  if (!(a.data = malloc(size * sizeof(*(a.data)))))
   return (-1);
    // so you will have a.data[0] or a.data[1] ...

    //   for malloc on 2d                                   
    //   if (!(a.data[0] = malloc(size * sizeof(*(a.data)))))                
    //    return (-1);                                                     
  return 0;
 }

But since you start malloc you must use free after you done with the tab

Otherwise, change it to boo data[] or data[][] would require a precise number of struct stocked before you compile.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!