如何解决从 Go 传递 C-struct 时出现 _Ctype_struct_0 错误
从 Go 开始,我需要通过传递 C-struct 对象来调用 C 函数。所有的业务逻辑都在 C 代码中。 调用后,go 会从 struct 对象中读取结果。
我是 C 程序员,正在尝试编写 POC。
示例 C 结构
struct Record{
uchar index[18];
uchar len[4];
ushort crc;
};
int readrecord(int fd,Record *c,char *buffer,int recsize);
GoCode
#include "rrecord.h"
*/
import "C"
import (
"fmt"
"unsafe"
)
func main(){
recptr := new(C.struct_Record)
buffer := C.malloc(C.sizeof_char * 101)
recsize := 100
len := C.readrecord(C.int(fd),recptr,(*C.char)(buffer),C.int(recsize))
cstr := C.GoString((*C.char)(buffer))
fmt.Printf("\n The lenght is :%d",len)
fmt.Printf("\n The data :%s",cstr)
}
我收到以下错误
cannot use recptr (type *_Ctype_struct_Record) as type *_Ctype_struct___1 in argument to _Cfunc_readrecord
我试图通过遵循 this question 中的信息来解决。但对我来说,我的代码看起来很好,无法解决上述错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。