[Numpy-discussion] record arrays with char*?

Christopher Jordan-Squire cjordan1 at uw.edu
Tue Feb 11 01:43:53 EST 2014

I'm trying to wrap some C code using cython. The C code can take
inputs in two modes: dense inputs and sparse inputs. For dense inputs
the array indexing is naive. I have wrappers for that. In the sparse
case the matrix entries are typically indexed via names. So, for
example, the library documentation includes this as input you could

 char* ind;
 double val, wght;
} data[] = { {"camera", 15, 2}, {"necklace", 100, 20}, {"vase", 90, 20},
                 {"pictures", 60, 30}, {"tv", 40, 40}, {"video", 15, 30}};

At the C level, data is passed to the function by directly giving its
address. (i.e. the C function takes as an argument (unsigned long)
data, casting the data pointer to an int)

I'd like to create something similar using record arrays, such as

np.array([("camera", 15, 2), ("necklace", 100, 20), ... ],

Unfortunately this fails because
(1) In cython I need to determine the address of the first element and
I can't take the address of a an input whose type I don't know (the
exact type will vary on the application, so more or fewer fields may
be in the C struct)
(2) I don't think a python object type is what I want--I need a char*
representation of the string. (Unfortunately I can't test this because
I haven't solved (1) -- how do you pass a record array around in
cython and/or take its address?)

Any suggestions?

More information about the NumPy-Discussion mailing list