dpkrbe395930 2018-02-12 02:37
浏览 88
已采纳

将切片转换为数组指针

I have this bit of code,

var buffers [2]uint32
gl.GenBuffers(2, /* What here! */)

gl.GenBuffers() asks for a int32 and a *uint32. In C, you'd pass an static array or a pointer to it and all would be good, but in Golang, a slice doesn't work...

Two ways I can think of are allocating a dynamic array, or converting the slice into a pointer array,

Bad thing is that I don't know how to do either... I tried new, but it doesn't really work like malloc... I'm stumped and any help would be greatly appeciated!

  • 写回答

1条回答 默认 最新

  • dsf11t5u1651 2018-02-12 02:44
    关注

    Write:

    var buffers [2]uint32
    gl.GenBuffers(int32(len(buffers)), &buffers[0])
    

    For example,

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        var buffers [2]uint32
        fmt.Printf("%d %p
    ", int32(len(buffers)), &buffers[0])
        fmt.Printf("%T %T
    ", int32(len(buffers)), &buffers[0])
    }
    

    Playground: https://play.golang.org/p/J-tKynWv5P1

    Output:

    2 0x10410020
    int32 *uint32
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!