# References in Julia

**URL:** <https://discourse.julialang.org/t/references-in-julia/33946>\
**Category:** General Usage\
**Created:** [January 29, 2020, 7:26pm UTC](https://discourse.julialang.org/t/references-in-julia/33946 "2020-01-29T19:26:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [January 29, 2020, 7:26pm UTC](https://discourse.julialang.org/t/references-in-julia/33946/1 "2020-01-29T19:26:30Z")

</div>

Is it possible to change the contents of a vector by means of a pointer? Of course I am aware of the following:

```nohighlight
julia> v = zeros(6);

julia> p = v
6-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0

julia> p[1] = 10
10

julia> v
6-element Array{Float64,1}:
 10.0
  0.0
  0.0
  0.0
  0.0
  0.0

```

But what I want is to point to a different position than the first one. For example, I realized I can do the following, but I do not know if this is the correct approach:

```nohighlight
julia> v = zeros(6);
julia> p = Ref(v)
Base.RefValue{Array{Float64,1}}([0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
julia> p[][1] = 1
1
julia> p[][6] = 6
6
julia> v
6-element Array{Float64,1}:
 1.0
 0.0
 0.0
 0.0
 0.0
 6.0

```

Then, what happens if I want to point `p` to a different position than the first one of `v`. Is that even possible? In C would be something like:

```nohighlight
p = &v[3];
p[2] = 1.0 //changes v

```

On the other hand, when performing the following instruction

```nohighlight
julia> p = v[1:3]
julia> p[1] = 2
2
julia> v
6-element Array{Float64,1}:
 1.0
 0.0
 0.0
 0.0
 0.0
 6.0

```

I am copying `v` values to a new allocated vector `p`, right?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [January 29, 2020, 7:48pm UTC](https://discourse.julialang.org/t/references-in-julia/33946/2 "2020-01-29T19:48:35Z")

</div>

Check out views: [Arrays · The Julia Language](https://docs.julialang.org/en/v1.5-dev/base/arrays/#Views-(SubArrays-and-other-view-types)-1). The docs were recently updated to add more description, and I think they are what you’re looking for.

---

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [January 29, 2020, 7:50pm UTC](https://discourse.julialang.org/t/references-in-julia/33946/3 "2020-01-29T19:50:06Z")

</div>

Yes! It seems that this is what I was looking for. Thanks!

However, if anyone has more ideas please let me know!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 29, 2020, 8:31pm UTC](https://discourse.julialang.org/t/references-in-julia/33946/4 "2020-01-29T20:31:39Z")

</div>

Yeah, a `view` seems like it does exactly what you want:

```julia
julia> v = [1, 2, 3, 4]
4-element Array{Int64,1}:
 1
 2
 3
 4

julia> v_1 = view(v, 1)
0-dimensional view(::Array{Int64,1}, 1) with eltype Int64:
1                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                      
julia> v_1[] = 10
10                                                                                                                                                                                                                                                                                                                                    

julia> v
4-element Array{Int64,1}:
 10
  2
  3
  4

```
