# How to convert a pointer to String back to a String?

**URL:** <https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121>\
**Category:** General Usage\
**Created:** [January 3, 2018, 4:37am UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121 "2018-01-03T04:37:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 3, 2018, 4:37am UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/1 "2018-01-03T04:37:12Z")

</div>

I can create a pointer to a `String` using `pointer` and if I have a pointer to a string what’s the way to “convert” it back? Basically create a String where the first char is pointed at by the `pointer`

```julia
s = "abc"
ptr = pointer(s)
# how do I convert the ptr back to a string? By pointing to it?

```

**Update**  
Actually, I am looking for a way where `xp` and `yp` are the same, so as to conserve memory

```julia
x = "abc"
xp = pointer(x)
y = unsafe_string(xp)
yp = pointer(y)
yp == xp # false

```

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [January 3, 2018, 4:40am UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/2 "2018-01-03T04:40:09Z")

</div>

```julia
julia> s = "abc"
"abc"

julia> p = pointer(s)
Ptr{UInt8} @0x0000000121d16058

julia> unsafe_string(p)
"abc"

```

Perhaps also of interest:

```julia
julia> unsafe_load(p)
0x61

julia> unsafe_load(p+1)
0x62

julia> unsafe_load(p+2)
0x63

julia> unsafe_load(p+3)
0x00

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 3, 2018, 4:45am UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/3 "2018-01-03T04:45:06Z")

</div>

Yep. Just don’t forget to use `Base.unsafe_string` or `import Base.unsafe_string`.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [January 3, 2018, 5:02am UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/4 "2018-01-03T05:02:21Z")

</div>

What version of Julia? In 0.6.1, `unsafe_string` doesn’t need to be imported.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 3, 2018, 5:19am UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/5 "2018-01-03T05:19:19Z")

</div>

I am using 0.6.2.

Also

```julia
x = "abc"
xp = pointer(x)
y = unsafe_string(xp)
yp = pointer(y)
yp == xp # false

```

Actually I was looking for a way so that `xp` and `yp` are the same.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [January 3, 2018, 5:27am UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/6 "2018-01-03T05:27:14Z")

</div>

`unsafe_string` makes a copy of the string, as you discovered. Why not just set `yp` equal to `xp` if you want two pointers to the same string?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 3, 2018, 5:31am UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/7 "2018-01-03T05:31:51Z")

</div>

My use case is string grouping. I have a Strings array but many of them point to the same underlying location. So instead of grouping strings by underlying value (slow!), I group them by their pointer (fast!), but then I still want the underlying strings-value again in the final step of the algorithm.

Anyway, I can just `unsafe_string` once for each groups I guess.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [January 3, 2018, 4:15pm UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/8 "2018-01-03T16:15:59Z")

</div>

> [@xiaodai](#):
>
> x = “abc”  
> xp = pointer(x)  
> y = unsafe\_string(xp)  
> yp = pointer(y)  
> yp == xp # false

Not sure if you want do this in production code 😛 :

```julia
julia> unsafe_pointer_to_objref(xp-8) === x
true

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 3, 2018, 4:43pm UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/9 "2018-01-03T16:43:02Z")

</div>

You generally don’t want to take pointers to strings like this. The main reason you’d want a pointer to a string is interacting with C for which you’ll want to use the `Cstring` type in the `ccall` signature, which checks that you don’t have embedded nulls and ensures that your string data is null terminated. See

[https://docs.julialang.org/en/stable/manual/calling-c-and-fortran-code/](https://docs.julialang.org/en/stable/manual/calling-c-and-fortran-code/)

Edit: if you just want a pointer and a length, then you’ll want to use `Ptr{UInt8}` for the pointer field and `Csize_t` for the length (according to the C API, of course).

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 3, 2018, 6:37pm UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/10 "2018-01-03T18:37:40Z")

</div>

What about interning the strings? There is a package now that does just that.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 4, 2018, 9:00pm UTC](https://discourse.julialang.org/t/how-to-convert-a-pointer-to-string-back-to-a-string/8121/11 "2018-01-04T21:00:38Z")

</div>

Yeah, I will at some point make an optimisation for that which is basically pointer based sorting/grouping (btw which at this stage is 40% faster than R’s string radixsort already).
