Created
June 6, 2022 16:50
-
-
Save unakatsuo/89d0cdf4134157662cab35a8b4798469 to your computer and use it in GitHub Desktop.
Copy from a pointer of Go fixed array to a C array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#include <stdlib.h> | |
#include <string.h> | |
struct AAA { | |
char a[10]; | |
}; | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"unsafe" | |
) | |
func main(){ | |
aaa := (*C.struct_AAA)(C.calloc(C.sizeof_struct_AAA, 1)) | |
// build fail: cannot convert aaa.a (type [32]_Ctype_int) to type unsafe.Pointer | |
//fmt.Printf("aaa.a=%p, &aaa.a[0]=%p\n", unsafe.Pointer(aaa.a), unsafe.Pointer(&aaa.a[0])) | |
fmt.Printf("&aaa.a=%p, &aaa.a[0]=%p\n", unsafe.Pointer(&aaa.a), unsafe.Pointer(&aaa.a[0])) | |
src := [10]byte{1,2,3,4,5,6,7,8,9,10} | |
psrc := &src | |
fmt.Printf("&src=%p, &scr[0]=%p\n", unsafe.Pointer(&src), unsafe.Pointer(&src[0])) | |
fmt.Printf("psrc=%p, &psrc[0]=%p, &psrc[1]=%p\n", unsafe.Pointer(psrc), unsafe.Pointer(&psrc[0]), unsafe.Pointer(&psrc[1])) | |
fmt.Printf("len(src)=%d, len(psrc)=%d\n", len(src), len(psrc)) | |
// Copy from Go fixed array pointer to C array. | |
C.memcpy(unsafe.Pointer(&aaa.a), unsafe.Pointer(psrc), C.size_t(len(psrc))) | |
fmt.Println(aaa.a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment