Accessing a pointer's value in Cython

7,129 views
Skip to first unread message

Vogon

unread,
Jan 8, 2010, 9:48:58 AM1/8/10
to cython-users
Hi,

Is there an alternative to the following syntax of accessing a
pointer's value:


cdef int i = 10
cdef int* ptr = &i

ptr[0] = 5 # value of i becomes 5

# *ptr = 5 # does not work

print(ptr[0])


The array indexing syntax becomes a bit tedious after a while,
especially when I need to modify hundreds of pointer values.

Dag Sverre Seljebotn

unread,
Jan 8, 2010, 10:05:50 AM1/8/10
to cython...@googlegroups.com

No.

Dag Sverre

Vogon

unread,
Jan 8, 2010, 10:15:37 AM1/8/10
to cython-users
A concise answer. Thank you.

On Jan 8, 5:05 pm, Dag Sverre Seljebotn <da...@student.matnat.uio.no>
wrote:

Cass

unread,
Jul 21, 2010, 2:39:47 PM7/21/10
to Vogon, cython...@googlegroups.com
I have a problem that is similar but more complicated.

In the .pyx file, I have a pointer to a C struct. I need to access the
contents of the struct.
From within the .pyx file, I have tried:

#1
cdef mystruct_t c_struct = <mystruct_t> *(<mystruct_t *> p_c_struct)
which gives the error "Expected an identifier or literal"

#2
I have also tried:
pythonObj.a = p_c_struct->structField
which also gives the error "Expected an identifier or literal"

#3
I have considered pointer arithmetic but mystruct_t contains a union.

What is the proper way to do this?

One thing to mention is that my p_c_struct is actually a Python int
type that I cast into a pointer on the way in.

Thanks in advance!

Cass

Robert Bradshaw

unread,
Jul 21, 2010, 2:47:01 PM7/21/10
to cython...@googlegroups.com
On Fri, Jan 8, 2010 at 8:15 AM, Vogon <jus...@gmail.com> wrote:
> A concise answer. Thank you.

To elaborate, Python already has meaning attached to the prefix *
operator (packing tuples). The index notation gets annoying, but
writing Cython is (or at least should be) more like writing Python
than writing C.

- Robert

Robert Bradshaw

unread,
Jul 21, 2010, 2:51:31 PM7/21/10
to cython...@googlegroups.com
On Wed, Jul 21, 2010 at 11:39 AM, Cass <css...@gmail.com> wrote:
> I have a problem that is similar but more complicated.
>
> In the .pyx file, I have a pointer to a C struct. I need to access the
> contents of the struct.
> From within the .pyx file, I have tried:
>
> #1
> cdef mystruct_t c_struct = <mystruct_t> *(<mystruct_t *> p_c_struct)
> which gives the error "Expected an identifier or literal"

Try

cdef mystruct_t c_struct = p_c_struct[0] # assuming p_c_struct is
of type mystruct_t*
cdef mystruct_t c_struct = (<mystruct_t *>p_c_struct)[0] # otherwise

> #2
> I have also tried:
> pythonObj.a = p_c_struct->structField
> which also gives the error "Expected an identifier or literal"

Cython automatically dereferences for you, just use

p_c_struct.structField

-> is illegal Python/Cython syntax.

> #3
> I have considered pointer arithmetic but mystruct_t contains a union.
>
> What is the proper way to do this?
>
> One thing to mention is that my p_c_struct is actually a Python int
> type that I cast into a pointer on the way in.

Sounds dangerous, but can work. Make sure you cast it to a (large
enough) C int, then a pointer.

- Robert

Cass

unread,
Jul 21, 2010, 2:56:38 PM7/21/10
to cython-users
Thank you so much! It worked brilliantly.
Reply all
Reply to author
Forward
0 new messages