title: CGO: How do you use pointers in Golang to access data from an array in C
body: <p>I'm writing an app for the windows platform using FFmpeg and it's golang wrapper goav, but I'm having trouble understanding how to use the C pointers to gain access to the data array they point to.</p>
  
  <p>I'm trying to get the data stored in the AVFrame class and use Go to write it to a file, and eventually a texture in OpenGl to make a video player with cool transitions.</p>
  
  <p>I think understanding how to cast and access the C data will make coding this a lot easier.</p>
  
  <p>I've stripped out all the relevant parts of the C code, the wrapper and my code, shown below:</p>
  
  <p>C code - libavutil/frame.h</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  
  typedef struct AVFrame {
  #define AV_NUM_DATA_POINTERS 8
      uint8_t *data[AV_NUM_DATA_POINTERS];
  }
  </code></pre>
  
  <p>Golang goav wrapper - I don't really know whats going on here with the unsafe.Pointers and casting but it gives me access to the underlying C code</p>
  
  <pre><code>package avutil
  
  /*
      #cgo pkg-config: libavutil
      #include &lt;libavutil/frame.h&gt;
      #include &lt;stdlib.h&gt;
  */
  import "C"
  import (
      "unsafe"
  )
  
  type Frame C.struct_AVFrame
  
  func AvFrameAlloc() *Frame {
      return (*Frame)(unsafe.Pointer(C.av_frame_alloc()))
  }
  
  func Data(f *Frame) *uint8 {
      return (*uint8)(unsafe.Pointer((*C.uint8_t)(unsafe.Pointer(&amp;f.data))))
  }
  </code></pre>
  
  <p>My Golang code</p>
  
  <pre><code>package main
  
  import "github.com/giorgisio/goav/avutil"
  
  func main() {
      videoFrame := avutil.AvFrameAlloc()
  
      data := avutil.Data(videoFrame)
  
      fmt.Println(data) // here i want the values from data[0] to data[7], but how?
  }
  </code></pre>