advanced-go-programming-book icon indicating copy to clipboard operation
advanced-go-programming-book copied to clipboard

about zero length array

Open lianyuzhe opened this issue 6 years ago • 1 comments

第二章,类型转换关于零长数组的,源代码贴图,零长数组在struct的最后一个,存在这样的问题 When a C struct ends with a zero-sized field, but the struct itself is not zero-sized, Go code can no longer refer to the zero-sized field. Any such references will have to be rewritten.

lianyuzhe avatar Feb 06 '19 06:02 lianyuzhe

C语言结构体中位字段对应的成员无法在Go语言中访问,如果需要操作位字段成员,需要通过在C语言中定义辅助函数来完成。对应零长数组的成员,无法在Go语言中直接访问数组的元素,但其中零长的数组成员所在位置的偏移量依然可以通过unsafe.Offsetof(a.arr)来访问。

文中是说不支持访问零长数组,因此出现错误是正常的。 而且并不是所有的C编译器都支持零长数组,但是这个问题和Go语言无关了。

chai2010 avatar Feb 06 '19 15:02 chai2010