doulin6448 2014-01-16 09:39
浏览 16
已采纳

将[]字符串转到C字符**

I started trying to integrate some C libraries into my Go code for a project using cgo and have come across a problem.

In one of the C functions I need to pass argv to a function call. In C argv is a pointer to an array of char strings (K&R C, §5.10) and I need to convert from a slice of strings to a char**.

I have had a good look, high and low for any information on how to do variable type conversions from Go to C but there appears to be next to no documentation. Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • douxiang3978 2014-01-16 10:01
    关注

    You'll need to create the array yourself. Something like this should do:

    argv := new([]*C.char, len(args))
    for i, s := range args {
        cs := C.CString(s)
        defer C.free(unsafe.Pointer(cs))
        argv[i] = cs
    }
    C.foo(&argv[0])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了