c-for-go icon indicating copy to clipboard operation
c-for-go copied to clipboard

How to read char* value from Deref()

Open ljfuyuan opened this issue 1 year ago • 2 comments

Hello, I got a C function returns struct like this:

typedef struct Result {
  char *version;
} Result;

struct Result init(void);

With generated go is

type Result struct {
	Version        []byte
	ref67033404    *C.Result
	allocs67033404 interface{}
}

And Deref() does't set the Len for the Version []byte, So is there any way to read Version out since i can not get the length,

Thanks.

ljfuyuan avatar Oct 17 '24 03:10 ljfuyuan

Hi, it doesn't know len as in C the pointer just points to beginning. So you have to read the len value from somewhere or manually discover it reading byes until you meet \x00. You you can mark this pointer as safe string and it will automatically detect \x00 while copying the value.

xlab avatar Oct 18 '24 13:10 xlab

Hi, it doesn't know len as in C the pointer just points to beginning. So you have to read the len value from somewhere or manually discover it reading byes until you meet \x00. You you can mark this pointer as safe string and it will automatically detect \x00 while copying the value.

Thanks a lot for your answer. I have tried various ways to add Tip in TRANSLATOR, but I still can't find a way to mark it as a safe string. Can you give me some tips? Thanks again.

ljfuyuan avatar Oct 19 '24 03:10 ljfuyuan