Using ccall

question
#1

I have a struct in C -

struct foo
{
char s[128];
};

I want to make a struct in julia that can be used with ccall as return type or as an argument i.e,

struct bar
s::Type
end

x = bar(“hello”)

ccall((:fun1,“lib”),bar,(),)
ccall((:fun2,“lib”),Int,(bar),x)

If it is possible what should be the type here ?

0 Likes

#2

NTuple{128,Cchar}

0 Likes

#3

I tried it but how do I convert a string to NTuple, should I add a ‘\0’ so that C can recognize it as end of string ?

0 Likes

#4

If you want to pass a string, you probably shouldn’t pass it as a struct like this and you probably also shouldn’t pass the struct by value.

Now if for whatever reason the library you use requires this, you need to do exactly what you do in C, i.e. (mem)c[o]py the string from the String (const char*) to the struct. bar is immutable so you need to call memcpy on a Ref of it, i.e. _x = Ref{bar}(); ccall(:memcpy, ...... _x, "hello", length("hello") + 1); x = _x

0 Likes

#5

Thanks !!

0 Likes