On Sun, Mar 29, 2009 at 8:12 PM, Tiago Pereira <[email protected]> wrote:
> Hello,
>
> I know this topic has been discussed quite a few times in this mailing
> list. I have read and tried many of the suggestions given on this list,
> but nevertheless I'm still stuck.
>
> Here's my problem: I want to use a C function that takes a two
> dimensional array, float **a. How to I pass a numpy 2d array to **float?
> For a 1D array case I have managed to make it work by passing <float
> *>a.data to a C function (where a is a numpy 1D array). But for the 2D
> case, if I pass <float **>a.data python either segfaults or gives Bus
> Error. And if I try to pass only <float *>a.data, it says it can't
> assign type 'float *' 'to float **'.

Yes, that's for your own good :)

It depends on your C function, but generally, when a function expects
a 2d array as a float**, it means it expects a pointer to an array
which keeps the *address* (hence float** -> pointer to an array of
pointers) of each row. Which means you have to create such an array
first (those kind of arrays are also called ragged arrays, because
each row can potentially be of a different size).

Something like (pseudo code):

# a is the original array, n rows/m rows, in C order, as a float*, b
is an array of pointers, that is defined as float** b
for i in range(nrows):
    b[i] = &a[i*m]

cheers,

David
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to