# How to reference a variable within a struct

**URL:** https://discourse.julialang.org/t/how-to-reference-a-variable-within-a-struct/76135
**Category:** General Usage
**Tags:** struct
**Created:** [February 10, 2022, 1:17am UTC](https://discourse.julialang.org/t/how-to-reference-a-variable-within-a-struct/76135 "2022-02-10T01:17:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)
#### Post date: [February 10, 2022, 1:17am UTC](https://discourse.julialang.org/t/how-to-reference-a-variable-within-a-struct/76135/1 "2022-02-10T01:17:59Z")

</div>

I understand that Julia doesn’t have “pointers” in the way that C++ does. However, I have stumbled upon an instance in which I feel like I really need something like a pointer that can refer to a variable from within a struct without actually copying that variable’s data into the struct itself. **I want to know if there is a better or more Julian way to accomplish this task.**

Here is a toy example that illustrates what I am trying to do. Suppose I have written a function that solves linear programs (LPs) in the form \max\lbrace{c^T x : Ax \leq b\rbrace}. The input data (c, A, b) for this function is given by the following struct:

```julia
struct LinearProgram
    c::Vector{Float64}
    A::Matrix{Float64}
    b::Vector{Float64}
end

```

Now, I am interested in what happens to the optimal x when a new constraint is added to this LP, so we have \max\lbrace{c^T x : Ax \leq b, \sum x\_i \leq t \rbrace} instead. In particular, I want to vary the value of t and store the associated x-results somewhere. However, I will be doing this for various instances of (c, A, b), so I would like to make sure that each result is paired with the underlying LP data. Therefore, I created the following struct to store my LP results:

```julia
struct LinearProgramResult
    lp::LinearProgram
    t::Float64
    x::Vector{Float64}
end

```

and now my LP solver function looks like this:

```julia
function solveparametriclinearprogram(lp::LinearProgram, t::Float64)
    # Solve the linear program
    # max c⋅x
    # s.t. Ax ≤ b
    # Σx ≤ t
    # and obtain the optimal solution x
    
    return LinearProgramResult(lp, t, x)
end

```

With this approach, I can store a big list of `LinearProgramResult`s without worrying about losing the underlying data. However, because each `LinearProgramResult` contains the entire `LinearProgram` itself, if I want to try out several values of t for a single LP, then this seems to be a waste of memory.

Therefore, I would like to replace the second line of the LP result struct with a _reference_ to an LP instead, with the understanding that the user modifies the referenced variable at her own risk.

```julia
struct LinearProgramResult
    lp::pointer(LinearProgram)
    t::Float64
    x::Vector{Float64}
end

```

**Is this a reasonable thing to want to do? Is there a better approach I should be using instead?**

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 10, 2022, 2:22am UTC](https://discourse.julialang.org/t/how-to-reference-a-variable-within-a-struct/76135/2 "2022-02-10T02:22:24Z")

</div>

Just make the Result mutable struct?.. btw the only thing that actually allocates are vector and matrix inside `LinearProgram`, but they are just references anyway:

```julia
julia> struct LinearProgram
           c::Vector{Float64}
       end

julia> struct LinearProgramResult
           lp::LinearProgram
       end

julia> l1 = LinearProgram([1,2,3]); L1 = LinearProgramResult(l1);

julia> L2 = LinearProgramResult(l1);

julia> push!(l1.c, 0)

julia> L2
LinearProgramResult(LinearProgram([1.0, 2.0, 3.0, 0.0]))

julia> L1
LinearProgramResult(LinearProgram([1.0, 2.0, 3.0, 0.0]))

```

so creating multiple `Results` from the same `LinearProgram` doesn’t copy recursively

---

<div class="post-metadata">

### Author: ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)
#### Post date: [February 10, 2022, 8:57am UTC](https://discourse.julialang.org/t/how-to-reference-a-variable-within-a-struct/76135/3 "2022-02-10T08:57:39Z")

</div>

Thank you, this is what I needed. Is this the right way of thinking about what making the struct mutable does? Namely, a mutable struct is a struct in which references variables at construction time are encoded as references to those variables rather than copies?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 10, 2022, 9:32am UTC](https://discourse.julialang.org/t/how-to-reference-a-variable-within-a-struct/76135/4 "2022-02-10T09:32:57Z")

</div>

No. Mutable structures are structrues where the individual properties can be rebinded to other objects. The mutability property is irrelevant here:

```julia
a = [1,2,3]
mutable struct Foo
    x::Array{Int64,1}
end
o = Foo(a)
a[2] = 20
o # changed
struct Goo
    x::Array{Int64,1}
end
o = Goo(a)
a[2] = 200
o # still changed

```

If you DON’T want to use pointers, use `copy` or `deepcopy`. See also [this tutorial](https://sylvaticus.github.io/IntroSPMLJuliaCourse/dev/01_-_JULIA1_-_Basic_Julia_programming/0102-types_and_objects.html#Mutability-property-of-Julia-objects) for more details…

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [February 10, 2022, 1:48pm UTC](https://discourse.julialang.org/t/how-to-reference-a-variable-within-a-struct/76135/5 "2022-02-10T13:48:18Z")

</div>

Maybe [this](https://discourse.julialang.org/t/is-it-appropriate-to-say-a-variable-is-a-label-or-box/46068/19) will help you to form a mental model

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 10, 2022, 2:02pm UTC](https://discourse.julialang.org/t/how-to-reference-a-variable-within-a-struct/76135/6 "2022-02-10T14:02:57Z")

</div>

Probably the key point here is that, when referencing the `LinearProgram` struct inside `LinearProgramResults`, you may be copying only scalars and references, but not the complete arrays contained in the `LinearProgram`:

```julia
julia> struct LinearProgram
           n::Int
           x::Vector{Float64}
       end

julia> lp = LinearProgram(3,zeros(3));

julia> struct LinearProgramResult
           lp::LinearProgram
           t::Float64
           x::Vector{Float64}
       end

julia> r = LinearProgramResult(lp,1.0,rand(3));

julia> lp.x[1] = -1
-1

julia> r
LinearProgramResult(LinearProgram(3, [-1.0, 0.0, 0.0]), 1.0, [0.05921563061434709, 0.7694750365923975, 0.0760104857787165])

```

Note that when one updated `lp.x` the `r.lp.x` array is updated, that is, they are the same array. Thus, `r.lp.x` is just a reference to the same array as `lp.x`.

Therefore, it won’t be that terrible if you just repeat `lp` inside every `LinearProgramResult` you construct, because the arrays won’t be copied. You have, however, to be careful exactly because of this, since mutating the value of one of this shared arrays anywhere will mutate them everywhere.

In other words, your initial tentative is not as bad as you would think, it won’t probably imply a huge memory footprint.
