swig: how to access C array in Python -


i have following variable in c code, storing variables in 2d array. trying access array in python. getting following info..how can retrieve data array

module.c

uin32_t  array_vaiable[10][100] 

module.h

extern uint32_t array_variable[10][100] 

module.i

%module mod %{     #include "module.h" %} %include "typemaps.i" %include "module.h" 
swig -python module.i  <swig object of type 'int (*)[100]'> 

it in general bad idea expose global variables , should use function exposing static information.

a crude solution:

module.h

extern uint32_t array_variable[10][100] uint32_t* cast(uint32_t (*a)[100]); 

module.cpp

uint32_t array_variable[10][100] = {{}}; uint32_t* cast(uint32_t (*a)[100]) {   return &(a[9][99]); } 

module.i

%module example %{      #include "test.h" %}  %include "stdint.i" %include "cpointer.i" %pointer_functions(uint32_t, uint32p);  %include "test.h" 

and following in python access e.g. first element

import example example.uint32p_assign(example.cast(example.cvar.array_variable),2) example.uint32p_value(example.cast(example.cvar.array_variable)) 

if want access other elements, can use carrays.i after cast'ed 2d-array 1d-array.


Comments