# How to access the value of a RefValue{Any} Julia variable in C?

**URL:** <https://discourse.julialang.org/t/how-to-access-the-value-of-a-refvalue-any-julia-variable-in-c/54606>\
**Category:** General Usage\
**Tags:** embedding\
**Created:** [February 4, 2021, 10:19am UTC](https://discourse.julialang.org/t/how-to-access-the-value-of-a-refvalue-any-julia-variable-in-c/54606 "2021-02-04T10:19:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanxp](https://avatars.discourse-cdn.com/v4/letter/f/b487fb/32.png) [@fanxp](https://discourse.julialang.org/u/fanxp)\
**Post date:** [February 4, 2021, 10:19am UTC](https://discourse.julialang.org/t/how-to-access-the-value-of-a-refvalue-any-julia-variable-in-c/54606/1 "2021-02-04T10:19:49Z")

</div>

Hi,

I use `RefValue{Any}` to protect a scalar (immutable type) inside the `IdDict`, according to [Embedding Julia · The Julia Language](https://docs.julialang.org/en/v1/manual/embedding/). But I don’t know how to use the `RefValue{Any}` variable ('b" in the following example) in C (access its value , add something to its value, etc.). Below is a simple example:

```nohighlight
#include <stdlib.h>
#include <julia.h>

int main(void) {
    jl_init();
    // gc protect
    jl_value_t * refs = jl_eval_string("refs = IdDict()");
    jl_function_t * setindex = jl_get_function(jl_base_module, "setindex!");
    jl_datatype_t* ref_type = (jl_datatype_t*)jl_eval_string("Base.RefValue{Any}");

    jl_value_t * a = jl_eval_string("[1.0,2.0,3.0,4.0]");
    jl_call3(setindex, refs, a, a);

    jl_value_t * b = 0;
    jl_value_t * ref_b = 0;
    {
        b = jl_box_float64(0.0001);
        JL_GC_PUSH1(&b);
        ref_b = jl_new_struct(ref_type, b);
        JL_GC_POP();
        jl_call3(setindex, refs, ref_b, ref_b);
    }

    jl_function_t * print = jl_get_function(jl_base_module, "show");
    jl_function_t * vcat = jl_get_function(jl_base_module, "vcat");

    printf("print a:\n");
    jl_call1(print, (jl_value_t *)a);
    printf("\n");
    printf("print ref_b:\n");
    jl_call1(print, ref_b);
    printf("\n");

    jl_value_t * c = jl_call2(vcat, (jl_value_t *)a, (jl_value_t *)ref_b);
    jl_call3(setindex, refs, c, c);
    printf("print vcat(a, ref_b):\n");
    jl_call1(print, c);
    printf("\n");

    jl_atexit_hook(0);
    return 0;
}

```

And the output is:

```julia
print a:
[1.0, 2.0, 3.0, 4.0]
print ref_b:
Base.RefValue{Any}(0.0001)
print vcat(a, ref_b):
Any[1.0, 2.0, 3.0, 4.0, Base.RefValue{Any}(0.0001)]

```

Basically, I want to protect immutable variable `b`. So I wrap it in `RefValue{Any}` as `ref_b`, and then push it to `refs`, which is an `IdDict`. But when I want to call `vcat` to concatenate `a` and `ref_b`, `ref_b` is `Base.RefValue{Any}(0.0001)` instead of 0.0001, I have googled extensivly but found nothing. How can I get the value of b (=0.0001) instead of `Base.RefValue{Any}(0.0001)` through `ref_b`?  
Thanks!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 5, 2021, 11:24am UTC](https://discourse.julialang.org/t/how-to-access-the-value-of-a-refvalue-any-julia-variable-in-c/54606/2 "2021-02-05T11:24:57Z")

</div>

You call `getindex` on it (`ref_b[]`).

---

<div class="post-metadata">

**Author:** ![fanxp](https://avatars.discourse-cdn.com/v4/letter/f/b487fb/32.png) [@fanxp](https://discourse.julialang.org/u/fanxp)\
**Post date:** [February 9, 2021, 9:00am UTC](https://discourse.julialang.org/t/how-to-access-the-value-of-a-refvalue-any-julia-variable-in-c/54606/3 "2021-02-09T09:00:26Z")

</div>

Thank you very much and sorry to reply so late.  
I complete my code according to your suggestion and everything works well. I attach my code below as an example to help others who have the same problem.

```nohighlight
#include <stdlib.h>
#include <julia.h>

int main(void) {
    jl_init();
    // gc protect
    jl_value_t * refs = jl_eval_string("refs = IdDict()");
    jl_function_t * setindex = jl_get_function(jl_base_module, "setindex!");
    jl_function_t * getindex = jl_get_function(jl_base_module, "getindex");
    jl_datatype_t* ref_type = (jl_datatype_t*)jl_eval_string("Base.RefValue{Any}");

    jl_value_t * a = jl_eval_string("[1.0,2.0,3.0,4.0]");
    jl_call3(setindex, refs, a, a);

    jl_value_t * b = 0;
    jl_value_t * ref_b = 0;
    {
        b = jl_box_float64(0.0001);
        JL_GC_PUSH1(&b);
        ref_b = jl_new_struct(ref_type, b);
        JL_GC_POP();
        jl_call3(setindex, refs, ref_b, ref_b);
    }

    jl_function_t * print = jl_get_function(jl_base_module, "show");
    jl_function_t * vcat = jl_get_function(jl_base_module, "vcat");

    printf("print a:\n");
    jl_call1(print, (jl_value_t *)a);
    printf("\n");
    printf("print ref_b:\n");
    jl_call1(print, ref_b);
    printf("\n");
    b = jl_call1(getindex, ref_b);
    JL_GC_PUSH1(&b);
    printf("print getindex(ref_b)\n");
    jl_call1(print, b);
    printf("\n");

    jl_value_t * c = jl_call2(vcat, (jl_value_t *)a, (jl_value_t *)b);
    jl_call3(setindex, refs, c, c);
    printf("print vcat(a, b):\n");
    jl_call1(print, c);
    printf("\n");

    jl_atexit_hook(0);
    return 0;
}

```

But I have another question. I call `getindex` on variable `ref_b` and get the data `b`(=0.0001). I think the memory storing the data `0.0001` is protected by `IdDict`, so I don’t need to call `JL_GC_PUSH1(&b)` again and this line is redundant in my example. Is my idea correct?
