[Matrix-SIG] Using contiguous arrays with normal C syntax

Tom Fenn ptfenn@yahoo.com
Mon, 1 Nov 1999 10:33:19 -0500


Hello:

I'm trying to use contiguous arrays using the normal C syntax.  Here's some
code that shows what I'm trying to do:


PyObject *myfunction(PyObject *self,PyObject *args) {
    PyObject *input;
    PyArrayObject *array
    double **data;
    double x;
    int i,j;

    PyArg_ParseTuple(args,"O",&input);
    array = (PyArrayObject *) PyArray_ContiguousFromObject(input,
                                                    PyArray_DOUBLE,2,2);
    data = (double **) array->data;
    for (i=0;i<10;i++) {
        for (j=0;j<10;j++) {
            x = data[i][j];
        }
    }
    Py_INCREF(Py_None);
    return Py_None;
}

The data[i][j] line causes a seg fault. If I rewrite the line inside the
loops as:

x = *(double *) (array->data+i*array->strides[0]+j*array->strides[1]);

then everything is fine.  Seems I don't understand the relationship between
pointers and arrays in C as well as I thought I did.  Can anyone tell me
what I'm doing wrong?

Tom Fenn





__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com