【发布时间】:2018-04-17 16:26:42
【问题描述】:
在 Cython 中,如何在不使用 Python 对象(例如 bytes 或 str)作为中间体的情况下转换生成 C 双精度的 C 字符串 (char *) 表示形式?
事实上,我已经在一个 C 扩展文件 (.pyx) 中定义了我的函数,如下所示:
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 : impossible de convertir de 'double' en 'char*'和C2168 : strcat nombre de paramètres de fonction intrinséque insuffisant的错误
请问我该如何解决这个问题?
【问题讨论】:
-
请阅读How to Ask 和help center,尝试提供minimal reproducible example 并解释为什么您的代码不起作用。如果您没有代码,则几乎没有机会从该站点获得帮助。
-
您的代码有未定义的行为。您需要避免在 c 中这样做,或者准确了解什么是 c 字符串以及它们是如何工作的。
-
我尽量简化函数的定义,因为在实际代码中比较复杂,里面有很多处理。
-
没关系。由于缺乏关于 c 字符串和内存管理的知识,代码存在一个根本错误。
-
对不起,我真的是 cython 代码的初学者:/