Vogon | 8 Jan 2010 15:48
Picon

Accessing a pointer's value in Cython

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 | 8 Jan 2010 16:05
Picon
Picon

Re: Accessing a pointer's value in Cython

On Fri, 2010-01-08 at 06:48 -0800, Vogon wrote:
> 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.

No.

Dag Sverre

Vogon | 8 Jan 2010 16:15
Picon

Re: Accessing a pointer's value in Cython

A concise answer. Thank you.

On Jan 8, 5:05 pm, Dag Sverre Seljebotn <da... <at> student.matnat.uio.no>
wrote:
> On Fri, 2010-01-08 at 06:48 -0800, Vogon wrote:
> > 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.
>
> No.
>
> Dag Sverre

Cass | 21 Jul 2010 20:39
Picon

Re: Accessing a pointer's value in Cython

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 | 21 Jul 2010 20:51
Favicon

Re: Re: Accessing a pointer's value in Cython

On Wed, Jul 21, 2010 at 11:39 AM, Cass <cssndrx <at> 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.
(Continue reading)

Cass | 21 Jul 2010 20:56
Picon

Re: Accessing a pointer's value in Cython

Thank you so much! It worked brilliantly.

Robert Bradshaw | 21 Jul 2010 20:47
Favicon

Re: Re: Accessing a pointer's value in Cython

On Fri, Jan 8, 2010 at 8:15 AM, Vogon <jusa.sj <at> 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

> On Jan 8, 5:05 pm, Dag Sverre Seljebotn <da... <at> student.matnat.uio.no>
> wrote:
>> On Fri, 2010-01-08 at 06:48 -0800, Vogon wrote:
>> > 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.
>>
(Continue reading)


Gmane