musbur
musbur

Reputation: 679

What is the correct type for PyArray_SimpleNewFromData()'s dims argument?

the numpy C API documentation gives this signature:

PyObject* PyArray_SimpleNewFromData(int nd, npy_intp* dims, int typenum, void *data)

Note that dims is essentially of type int**. However, all examples I found for the usage of this and similar functions pass type int*, for example here.

When passing a pointer to integer my code works fine, but the compiler complains about pointer type mismatch of the "dims" argument.

PyObject *arr;

import_array();

npy_float d[] = {1, 2, 3, 4};
npy_intp dims[] = {sizeof d / sizeof *d};
arr = PyArray_SimpleNewFromData(1, dims, NPY_FLOAT, d);

From my understanding of how the function works, I believe int* should be the right type because it is just an input parameter that informs the function about how many entries each array dimension has. I don't understand why both the documentation and the compiler expect int** event though int* works.

So how is it done right?

Upvotes: 0

Views: 402

Answers (1)

user2357112
user2357112

Reputation: 280973

Your confusion seems to stem from a misunderstanding of what npy_intp is. It's not a typedef for int *. It's an integer type big enough to hold a pointer.

Upvotes: 1

Related Questions