When I ran this code, I expected to printing result like A: 4, B: 89
. But actually, Does not display nothing.
Why this program does not display result to stdout?
main.go:
package main
/*
#include "c.h"
*/
import "C"
import (
"unsafe"
)
type S struct {
A int
B int
}
func main() {
s := &S{A: 4, B: 89}
pass_to_c := (*C.S)(unsafe.Pointer(s))
C.gostruct(pass_to_c)
}
c.h
#include <stdio.h>
#include <stdlib.h>
typedef struct {
long int A;
long int B;
} S;
extern void gostruct(S *struct_s) {
printf("A: %ld, B: %ld
", struct_s->A, struct_s->B);
}
Thanks for comments.
I can got expected result with below codes
main.go:
package main
/*
#include "c.h"
*/
import "C"
import (
"unsafe"
)
type S struct {
A int64 // 64bit int
B int64 // 64bit int
}
func main() {
s := &S{A: 4, B: 89}
pass_to_c := (*C.S)(unsafe.Pointer(s))
C.gostruct(pass_to_c)
}
c.h:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
long long int A; // 64bit int
long long int B; // 64bit int
} S;
extern void gostruct(S *struct_s) {
printf("{A: %lld, B: %lld}
", struct_s->A, struct_s->B);
}
I suppose struct field must use same type between languages. In question code, struct fields type are not same. (C struct: 32bit int, Go struct: 64bit int)
In answer code, struct field is same between language. (both struct: 64bit int)
Note that My architecture is darwin/amd64