# Using ccall

**URL:** <https://discourse.julialang.org/t/using-ccall/22254>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 23, 2019, 6:19pm UTC](https://discourse.julialang.org/t/using-ccall/22254 "2019-03-23T18:19:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aditya](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@aditya](https://discourse.julialang.org/u/aditya)\
**Post date:** [March 23, 2019, 6:19pm UTC](https://discourse.julialang.org/t/using-ccall/22254/1 "2019-03-23T18:19:46Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 23, 2019, 6:53pm UTC](https://discourse.julialang.org/t/using-ccall/22254/2 "2019-03-23T18:53:04Z")

</div>

`NTuple{128,Cchar}`

---

<div class="post-metadata">

**Author:** ![aditya](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@aditya](https://discourse.julialang.org/u/aditya)\
**Post date:** [March 23, 2019, 7:34pm UTC](https://discourse.julialang.org/t/using-ccall/22254/3 "2019-03-23T19:34:02Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 23, 2019, 7:44pm UTC](https://discourse.julialang.org/t/using-ccall/22254/4 "2019-03-23T19:44:37Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![aditya](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@aditya](https://discourse.julialang.org/u/aditya)\
**Post date:** [March 23, 2019, 8:08pm UTC](https://discourse.julialang.org/t/using-ccall/22254/5 "2019-03-23T20:08:51Z")

</div>

Thanks !!
