Re: error: conversion to non-scalar type requested

Lenard Lindstrom <[email protected]>
Newsgroups gmane.comp.python.pyrex
Message-ID <[email protected]>
Sven Berkvens-Matthijsse wrote:
>> Now I've not the code to try this, but I think it doesn't solve the
>> problem.  func function needs a struct in_addr, not a pointer to
>> struct in_addr, so the cast is correct. Maybe the problem is in the
>> generated C code. I'll post the original pyrex (and C generated)
>> code that originate this error very soon to give you more details.
>>     
>
> No, what Lenard said is correct, the cast binds stronger than the []
> operator. What you said:
>
> 	tmp = <in_addr>tmpinPtr[0]
>
> actually reads as:
>
> 	tmp = (<in_addr>tmpinPtr)[0]
>
> which of course does not work. What Lenard says reads as:
>
> 	tmp = (<in_addr *>tmpinPtr)[0]
>
> which is correct. Of course, you can write it your way, just be sure
> to use parentheses in that case, like this:
>
> 	tmp = <in_addr>(tmpinPtr[0])
>
>   
Actually, if I understood the original question correctly, the function 
getIstance() returns a pointer as a C long. So first the long must be 
recast as a pointer:

      <in_addr *>tmpinPtr

Then the pointer must be dereferenced to get an in_addr value:

      (<in_addr *>tmpinPtr)[0]

The parenthesis are required. When looking up operator precedence I 
confused the cast operator with the function call. Also tmpinPtr must be 
declared as long. The following Pyrex module compiles and runs.

++++++++++++++++++++++++++++++++++++++++
ctypedef struct in_addr:
    int i
    double d

cdef in_addr instance
cdef in_addr tmp
cdef long tmpinPtr
instance.i = 1
instance.d = 3.14

def getInstance():
    return <long>&instance

cdef func(in_addr tmp):
    print tmp.i, tmp.d

tmpinPtr = getInstance()
tmp = (<in_addr *>tmpinPtr)[0]
func(tmp)
++++++++++++++++++++++++++++++++++++++++

-- 
Lenard Lindstrom
<[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.