Passing multidimensional arrays to C functions in CGO

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package main/* #include &LT;STDIO.H&G                                                                                                                                                                                                T                                                                                                                                                  #include <string.h>                                                                                                              void Fill_array (char *s) {                                                                              strcpy (S, "Cobbliu");                                                                                                                                                                                                      }                                                                                                                                                                      void Fill_2d_array (char **arr, int columesize) { strcpy ((char*) (arr+                                                                                                                                                          0*sizeof (char) *columesize), "Hello"); strcpy ((char*) (arr+1*sizeof (char) *columesize/sizeof (          char*)), "CGO");                                                                                                                          } */import "C" import "FMT" imp Ort "unsafe" func main () {var dir [10]byte C.fill_array ((*c.char) (unsafe. Pointer (&dir[0])) FMT.                                                                                                                                                                                            Println (String (dir[:]))//var dirs [4][16]byte Dir S: = make ([][]byte, 4) for I: = 0; I < 4; i++ {Dirs[i] = make ([]byte, +)} c.fill_2d_array ((**c.char) (unsafe. Pointer (&dirs[0][0])), C.int (+)) fmt. Println (dirs)}

It is important to note that slice can not be converted like this: (*c.char) (unsafe. Pointer (&dir)), because slice is actually not a fully-meaningful array in go, it's just a data structure with several heads, see http://blog.golang.org/ Go-slices-usage-and-internals, if the direct &dir, that is equivalent to the DIR this data structure address at the location of the conversion to *c.char, so that the consequences are completely non-period, run-time core off is the result of normal.

Https://groups.google.com/forum/#!topic/golang-nuts/Nb-nfVdAyF0 here is a discussion of multidimensional arrays, see the LAN classmate's reply, for C and go in the multi-dimensional array type of elaboration more clear.

enjoy!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.