Need to cast a GO structure to C structure, and print the populated GO contents in C code

I have populated a structure in go Lang, i want to pass this structure to C and i want to print contents of the structure populated in GO in C.

But when i try this i get following error.

could not determine kind of name for C.GetStruct

gcc errors for preamble:
./t32.go:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

Can anyone suggest what am doing wrong, am new to GO and am trying out sample GO to C interaction and C to GO interaction.

Here is my code.

 └── t32
    ├── remote.c
    ├── t32.go
    └── t32.h

remote.c

#include "t32.h"

int GetStruct (Address *addr)
{
    printf("Address_0:%d\n", addr.Address_0);
    printf("Address_1:%d\n", addr.Address_1);
    printf("Address_2:%d\n", addr.Address_2);
    printf("Address_3:%d\n", addr.Address_3);
    return 0;
}

t32.go

package t32

import (
    /*
        t32.h
    */
    "C"
    "errors"
    "fmt"
    "unsafe"
)

func SendStruct() {

    bps := make([]Ctype_struct_Breakpoint, 1)
    bps.Address_0 = 192
    bps.Address_1 = 168
    bps.Address_2 = 25
    bps.Address_3 = 51
    C.GetStruct((*C.struct_T32_Breakpoint)(unsafe.Pointer(&bps[0])))
}

t32.h

#ifndef __T32_H__
#define __T32_H__


typedef struct {
    int Address_0
    int Address_1
    int Address_2
    int Address_3
} Address;


int GetStruct( Address* );

#endif /* __T32_H__ */

This looks like gcc is trying to compile the Go code:

This does not look like valid Go (either bps is a slice or a struct, it cannot be both):

So all in all I’m not really sure what you’re trying to do, what the code really looks like, or how you’re building. :slight_smile:

The code you have supplied is wrong in many areas.

You are not defining your struct or function in C properly, and you are also not accessing its members correctly. You are also attempting to use a struct named Breakpoint which does not exist in the C code you provided.

If you read the cgo documentation, you would know you need to call the C struct using C.struct_Address

unsafe.Pointer is void * in C, and no idea why your type casting to a struct your C function is not expecting.

Working code below:

t32.go

package t32

//#include "t32.h"
import "C"

func SendStruct() {
    var bps C.struct_Address
    bps.Address_0 = 192
    bps.Address_1 = 168
    bps.Address_2 = 25
    bps.Address_3 = 51
    C.GetStruct(&bps)
}

t32.h

#ifndef __T32_H__
#define __T32_H__

struct Address {
    int Address_0;
    int Address_1;
    int Address_2;
    int Address_3;
};

int GetStruct(struct Address *addr);

#endif /* __T32_H__ */

remote.c

#include "t32.h"
#include <stdio.h>

int GetStruct (struct Address *addr)
{
    printf("Address_0:%d\n", addr->Address_0);
    printf("Address_1:%d\n", addr->Address_1);
    printf("Address_2:%d\n", addr->Address_2);
    printf("Address_3:%d\n", addr->Address_3);
    return 0;
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.