# Initializing a Vector{Cuchar} (string) prior to calling a C function

**URL:** https://discourse.julialang.org/t/initializing-a-vector-cuchar-string-prior-to-calling-a-c-function/63991
**Category:** New to Julia
**Created:** [July 3, 2021, 4:17pm UTC](https://discourse.julialang.org/t/initializing-a-vector-cuchar-string-prior-to-calling-a-c-function/63991 "2021-07-03T16:17:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mmv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmv/32/26323_2.png) [@mmv](https://discourse.julialang.org/u/mmv)
#### Post date: [July 3, 2021, 4:17pm UTC](https://discourse.julialang.org/t/initializing-a-vector-cuchar-string-prior-to-calling-a-c-function/63991/1 "2021-07-03T16:17:02Z")

</div>

Hi! I m declaring a 28 characters-long string variable before calling a C program as follows:

```julia
julia> datetime = Vector{Cuchar}(undef, 28)
28-element Vector{UInt8}:
 0x01
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
    ⋮
 0x09
 0x01
 0x00
 0x00
 0x00
 0x70
 0xbd
 0xcf
 0x0e

```

Clearly, that memory location does contain non-null bytes in various places. When the C function returns the (correct) result, I get

```julia
julia> datetime
28-element Vector{UInt8}:
 0x32
 0x30
 0x30
 0x32
 0x2d
 0x30
 0x35
 0x2d
 0x30
 0x32
    ⋮
 0x5a
 0x00
 0x00
 0x00
 0x00
 0x70
 0xbd
 0xcf
 0x0e

julia> dt = String(datetime)
"2002-05-02T02:00:00Z\0\0\0\0p\xbd\xcf\x0e"

```

Question: How can I initialize the memory location originally reserved for `datetime` in such a way (1) that each and every byte is a null byte, and (2) that the outcome would be `"2002-05-02T02:00:00Z\0\0\0\0\0\0\0\0"`?  
Thanks for suggestions.

---

<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: [July 3, 2021, 6:41pm UTC](https://discourse.julialang.org/t/initializing-a-vector-cuchar-string-prior-to-calling-a-c-function/63991/2 "2021-07-03T18:41:46Z")

</div>

> [@mmv](#):
>
> How can I initialize the memory location originally reserved for `datetime` in such a way (1) that each and every byte is a null byte

`datetime = zeros(Cuchar, 28)`

If the string is NUL-terminated and you want to convert it to a corresponding `String`, but _not_ including the NUL character(s) (i.e. if you want `"2002-05-02T02"` in your case), you can do e.g.

```julia
dt = String(datetime[1:findfirst(iszero, datetime)-1])

```

or alternatively (using lower-level operations to avoid copying data unnecessarily).

```julia
dt = GC.@preserve datetime unsafe_string(pointer(datetime))

```

---

<div class="post-metadata">

### Author: ![mmv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmv/32/26323_2.png) [@mmv](https://discourse.julialang.org/u/mmv)
#### Post date: [July 3, 2021, 8:05pm UTC](https://discourse.julialang.org/t/initializing-a-vector-cuchar-string-prior-to-calling-a-c-function/63991/3 "2021-07-03T20:05:21Z")

</div>

Thanks a lot, @stevengj: That is also an elegant way to initialize that variable.
