使用cython将双精度型转换为char * [英] Convert a double to char* with cython
问题描述
在Cython中,如何在不使用Python对象(例如 bytes <的情况下)转换以生成C的C字符串(
char *
)表示形式/ code>或 str
)作为中间物?
In Cython, how can I convert produce a C string (char *
) representation of a C double without using Python objects (e.g. bytes
or str
) as an intermediate?
事实上,我已经在C扩展文件(.pyx),如下所示:
In fact, I have defined my function in a C-extension file (.pyx) as follows:
cdef void function(self, char* var1) nogil:
cdef char* chaine =""
cdef double inter = 0.0
#Here there is some treatment which modifies the value of the local variable 'inter' so that it contains a double value different from 0
strcat(chaine , "(")
strcat(chaine , <char*>inter)
strcat(chaine , ")**beta")
strcpy(&var1, chaine)
编译文件后,我有了错误 C2440:不可能将'double'转换为'char *'
和 C2168:strcat nombre deparamètres的功能固有不足
After compilation of the file, I have the errors C2440 : impossible de convertir de 'double' en 'char*'
and C2168 : strcat nombre de paramètres de fonction intrinséque insuffisant
如何我可以解决问题了吗?
How can I fix the problem please?
推荐答案
抛开是否值得在python或C上这样做的问题在您的示例代码中,似乎在C级别上存在一些严重的误解。有很多掩饰,所以我将只提供一些指导以帮助您朝正确的方向发展。一旦对C和cython感到更满意,请随时发布正确的代码版本作为答案。
Setting aside the issue of whether or not it even is worth doing this at the python or C level, it looks like there are several critical misunderstandings at the C level presented in your sample code. There is a lot of cover, so I will just give few pointers to help steer you in the right direction; feel free to post a corrected version of your code as an answer once you feel more comfortable with C and cython.
首先,一个有关指针的词。指针只是保存内存地址的变量。该内存地址指向内存中的某些内容。以下是一个可能清除此问题的简单示例:
First, a word about pointers. A pointer is just a variable that holds a memory address. This memory address "points" to some contents in memory. Here is a simple sample that might clear this up:
cdef int a_number = 42#just a regular int, nothing special here :)
cdef int* a_pointer = &a_number#"referencing" to get the address of a_number
cdef int b_number = a_pointer[0]#"dereferencing" to get the value at a_pointer
#note the dereferencing syntax is different in cython than in C!!!
第二个是函数的工作方式。在C语言中,一切都是传递值。这意味着每当您将参数传递给函数时,都会对参数进行副本,并对该副本进行操作。这包括指针;如果您像尝试使用函数
那样尝试设置 var1
指针,则实际的 var1
指针不变,并且仅修改了 function
范围内的本地副本。显然,这不是我们想要的!
Second is how functions work. In C, everything is pass-by-value. This means that whenever you pass parameters to a function, a copy of the parameter is made and operations take place on this copy. This includes pointers; if you try to set your var1
pointer like you are attempting in function
, the actual var1
pointer is unchanged and only a local copy within the scope of function
is being modified. Clearly not what we want!
第三,我们需要查看字符串如何用C表示。字符串基本上是您关心的字符列表,后跟一个null终止符 \0
。我敢肯定,您可以在线上阅读许多有关 char *
和 char []
之间区别的资料,我强烈建议您看看它们。我在这里只说 char *
只是一个指针,因此它仅指向第一个字符。 char *
也没有字符串长度的概念。
Third, we need to see how strings are represented in C. Strings are basically a list of characters that you care about, followed by a null terminator \0
. I am sure there are lots of sources you can read online about the difference between say char*
and char[]
, and I strongly suggest you take a look at them. I will just say here that char*
is just a pointer, and so it only points to the first character. char*
also has no concept of the length of the string.
一旦您能很好地处理所有字符串这些概念之后,您就可以开始查看Linux手册页上的功能,例如 strcpy 和 strcat 。我还会查找 sprintf ,这有点相似python的 format
格式,可能比将一堆碎片串联在一起更聪明。希望这对您的学习之旅有所帮助,祝您好运!
Once you have a good handle of all of these concepts, you can then start looking at functions on the linux man pages like strcpy and strcat. I would also look up sprintf, which is sort of similar to python's format
and might be smarter than concatenating a bunch of pieces together. Hope this helps you on your learning journey, and good luck!
这篇关于使用cython将双精度型转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!