如何从Go发送字节数组到C

下巴迪杜:

我试图将字节数组从GO传递给C函数,但是我做不到,有我的代码:

package main
 /*

 #include <stdint.h>
 #include "api.h"
 #include "parameters.h"
 #include "lilliput-ae.h"
 #include "tool.h"


 void print(void *b)
  {
    printf("%d",b[0]);
    printf("%d",b[5]);
  }


  */
  import "C"
  import "unsafe"



   func main() {

     a := [16]byte{16, 8, 7, 4, 12, 6, 7, 8, 9, 10, 11, 7, 16, 14, 15, 1}
     ptr := unsafe.Pointer(&a[0])
     C.print(ptr)
   }

我的最终目标是打印uint8_t数组之类的C代码,当我成功完成此操作时,我将尝试将数组从C代码发送到Go。

peterSO:

我正在将字节数组从Go传递给C函数。

我的目标是打印类似于uint8_t数组的C代码


例如,

package main

/*
#include <stdio.h>
#include <stdint.h>

void print(void *p) {
    uint8_t *b = p;
    printf("%d ",b[0]);
    printf("%d ",b[5]);
    printf("\n");
}
*/
import "C"

import (
    "fmt"
    "unsafe"
)

func main() {
    a := [16]byte{16, 8, 7, 4, 12, 6, 7, 8, 9, 10, 11, 7, 16, 14, 15, 1}
    fmt.Println(a)
    ptr := unsafe.Pointer(&a[0])
    C.print(ptr)
}

输出:

[16 8 7 4 12 6 7 8 9 10 11 7 16 14 15 1]
16 6 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章