Hello
I'm looking for help for the following snippet
let sq = @["hello", "nim"]
proc cFunction(a1: pointer) =
# How to cast the pointer to seq[string]
proc main() =
# How to call cFunction and send sq as a c void pointer
I have most of the time type mismatch when using
cFunction(cast[pointer](sq[0].addr))
cFunction(sq.unsafeAddr)
cFunction(sq[0].unsafeAddr)
cFunction(sq[0].unsafeAddr.pointer)
Well, C can understand it. But that does not mean you should want it to :)
{.emit: """
typedef long long int nim_int;
struct nim_seq_hdr {
nim_int len;
nim_int reserved;
};
struct nim_string {
struct nim_seq_hdr seq;
char *data[0];
};
struct nim_string_seq {
struct nim_seq_hdr seq;
struct nim_string *data[0];
};
void foo(void *ptr)
{
struct nim_string_seq *n = *(struct nim_string_seq **)ptr;
nim_int i;
for(i=0; i<n->seq.len; i++) {
struct nim_string *s = *(struct nim_string **)(&n->data[i]);
printf("%d -> %d: %s\n", i, s->seq.len, s->data);
}
}
"""}
proc foo(a1: pointer) {.cdecl, importc.}
var a = @["one", "two", "three"]
foo(addr a)