cgo – How to convert string to C fixed char array

huangapple go评论88阅读模式
英文:

cgo - How to convert string to C fixed char array

问题

我正在尝试在我的Go代码中实例化一个C结构体。
该结构体的定义如下(在一个我无法修改的外部库中):

typedef struct {
    char field1[256];
} S1;

在Go中,我这样做:

func myfunc(mystr string){
    // 我们需要将mystr从字符串转换为字符数组
    cStr := C.CString(mystr)
    defer C.free(unsafe.Pointer(cStr))

    // 现在应该可以工作了
    s1 := &C.S1{field1: cStr}

    // 对s1进行操作...
}

但是它无法编译,因为:

> 无法将cStr(类型为*C.char)作为字段值中的[256]C.char类型使用

我尝试了强制转换([256]C.char)(cStr),但显然也不起作用。

有没有办法实现我想做的事情?

英文:

I'm trying to instantiate a C struct inside my Go code.
The struct is defined like this, (in an external library that I cannot modify):

typedef struct {
    char field1[256];
} S1

In go, I did this:

func myfunc(mystr string){
    // We need to convert mystr from string to char array
    cStr := C.CString(mystr)
    defer C.free(unsafe.Pointer(cStr)

    // Should work now
    s1 := &C.S1{field1: cStr}

    // Do something with s1...
}

But it doesn't compile because:

> cannot use cStr (type *C.char) as type [256]C.char in field value

I've tried forcing ([256]C.char)(cStr) but it obviously doesn't work either.

Is there a way to achieve what I'm trying to do?

答案1

得分: 3

最简单的解决方案是将结构体的字段定义更改为指向char的指针,这在C语言中是相当标准的字符串表示方式:

typedef struct {
    char *field1;
} S1;

更复杂的解决方案如下:

arr := [256]C.char{}

for i := 0; i < len(mystr) && i < 255; i++ { // 将第256个元素保持为零
    arr[i] = C.char(mystr[i])
}

s1 := &C.S1{field1: &arr[0]}

注意:以上代码未经测试,在此工作站上无法编译。

英文:

The simplest solution is to change your struct's field definition to a char-pointer which is pretty standard for strings in C:

typedef struct {
    char *field1;
} S1

The more complex solution would be <sup>[1]</sup>:

arr := [256]C.char{}

for i := 0; i &lt; len(mystr) &amp;&amp; i &lt; 255; i++ { // leave element 256 at zero
    arr[i] = C.char(mystr[i])
}

s1 := &amp;C.S1{field1: arr}

<sup>[1]</sup> Code untested, cannot compile on this workstation.

答案2

得分: 3

很遗憾,在Go语言中没有处理[size]C.char作为字符串的便捷方法(我记得曾经看到过一个提案来添加这个功能...)。

在我的代码中,我选择在需要时手动将字符串写入结构体中,可以使用以下方法:

func strCopy(dest *[maxTextExtent]C.char, src []byte) {
    for i, c := range src {
        dest[i] = C.char(c)
    }
    // 这是C语言,我们需要终止字符串!
    dest[len(src)] = 0
}

而我以前使用的方法,相对不太安全:

s1 := &C.S1{
    field1: *(*[256]C.char)(unsafe.Pointer(cStr)),
}
英文:

Unfortunately there aren't any convenience methods for handling [size]C.char as a string in Go (I think I saw a proposal to add this at one point though...)

In my code instead of handling it directly, I opted to manually write the string into the struct when needed with some like

func strCopy(dest *[maxTextExtent]C.char, src []byte) {
	for i, c := range src {
		dest[i] = C.char(c)
	}
	// This is C, we need to terminate the string!
	dest[len(src)] = 0
}

And the way I used to handle it, which is much less safe is

s1 := &amp;C.S1{
	field1: *(*[256]C.char)(unsafe.Pointer(cStr)),
}

huangapple
  • 本文由 发表于 2014年8月15日 21:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/25327313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定