FAQ
Hello

I'mfacing the following problem when accessing a C structure from a go
program. I have defined a C strucutre like this

/*
  typedef struct {
         int I;
         int J;
         int K;
         char *Name;
}Child
*/
Import "C"

type Child C.Child


Later I try to create a variable of type Child

var child = Child{10,20,30,C.CString("child")}



But I get this error

cannot use _Cfunc_CString("child") (type *C.char) as type [4]byte in field
value


If I remove one of the int fields, it works. If I remove the last char *
field, keeping the three int fields, it works.

Am I doing something wrong or is this a bug with cgo? thanks in advance

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Andy Balholm at Jun 25, 2015 at 6:59 pm
    I’m going to guess that there is a 4-byte padding field in the struct. Try putting the field names in your structure literal.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Pablo Chacin at Jun 25, 2015 at 9:39 pm
    Sorry for what I guess is a basic question, but what you mean by putting
    the field names in the structure literal? Thanks
    On Jun 25, 2015 20:59, "Andy Balholm" wrote:

    I’m going to guess that there is a 4-byte padding field in the struct.
    Try putting the field names in your structure literal.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andy Balholm at Jun 26, 2015 at 3:12 am

    what you mean by putting the field names in the structure literal?
    What Michael Gehring did in his answer:
    var child = Child{I: 10, J: 20, K: 30, Name: C.CString("child")}
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Pablo Chacin at Jun 29, 2015 at 3:03 pm
    Ok. it worked. Thanks (haven't seen Michael's answer)
    On Friday, June 26, 2015 at 5:13:02 AM UTC+2, Andy Balholm wrote:

    what you mean by putting the field names in the structure literal?


    What Michael Gehring did in his answer:

    var child = Child{I: 10, J: 20, K: 30, Name: C.CString("child")}
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michael Gehring at Jun 25, 2015 at 7:01 pm

    On Thu, Jun 25, 2015 at 06:52:31AM -0700, Pablo Chacin wrote:
    Hello

    I'mfacing the following problem when accessing a C structure from a go
    program. I have defined a C strucutre like this

    /*
    typedef struct {
    int I;
    int J;
    int K;
    char *Name;
    }Child
    */
    Import "C"

    type Child C.Child


    Later I try to create a variable of type Child

    var child = Child{10,20,30,C.CString("child")}
    This works:

      var child = Child{I: 10, J: 20, K: 30, Name: C.CString("child")}

    An anonymous padding field (the [4]byte) is inserted between K and Name.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 25, '15 at 6:46p
activeJun 29, '15 at 3:03p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase