# How to create a Cstring from a String?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-cstring-from-a-string/98566>\
**Category:** General Usage\
**Tags:** strings, interoperability, c\
**Created:** [May 9, 2023, 5:05pm UTC](https://discourse.julialang.org/t/how-to-create-a-cstring-from-a-string/98566 "2023-05-09T17:05:25Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 9, 2023, 6:00pm UTC](https://discourse.julialang.org/t/how-to-create-a-cstring-from-a-string/98566/2 "2023-05-09T18:00:33Z")

</div>

> [@heyx3](#):
>
> `clipboard_get(::Ptr{Cvoid})::Cstring = string(clipboard())`

How do you want to manage the memory for this? If Julia allocates a `String` object and returns a pointer to the data to C (which is possible via `unsafe_convert`), then the C code could crash if Julia garbage-collects the `String` before the C pointer is used (hence the “unsafe”). If you are okay with this — if you know for certain that the Julia string will not be garbage-collected before you use it (e.g. because you hold onto a reference to it), then you can call `Base.unsafe_convert(Cstring, s)` on `s::String`.

If C expects a `malloc`-ed string that **C** will take responsibility to `free`, then you’ll need to explicitly call `malloc` and copy the data yourself. e.g.

```julia
function malloc_cstring(s::String)
    n = sizeof(s)+1 # size in bytes + NUL terminator
    return GC.@preserve s @ccall memcpy(Libc.malloc(n)::Cstring,
                                        s::Cstring, n::Csize_t)::Cstring
end

```

Conversely, when you declare an _argument_ of a `ccall` as a `Cstring` (as is done in the call to `memcpy` here), then Julia can simply pass a pointer to the string data (assuming the data only needs to stick around for the duration of the `ccall`), first checking that the string contains no `NUL` bytes (as otherwise NUL-termination will not work; internally, Julia `String` data is already NUL-terminated so that it can be passed to C functions without copying).

---

_[View the full topic](https://discourse.julialang.org/t/how-to-create-a-cstring-from-a-string/98566)._
