# Get the output of a Julia function called from C

**URL:** https://discourse.julialang.org/t/get-the-output-of-a-julia-function-called-from-c/90699
**Category:** General Usage
**Tags:** c
**Created:** [November 23, 2022, 10:43am UTC](https://discourse.julialang.org/t/get-the-output-of-a-julia-function-called-from-c/90699 "2022-11-23T10:43:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![nvenkov1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvenkov1/32/43679_2.png) [@nvenkov1](https://discourse.julialang.org/u/nvenkov1)
#### Post date: [November 23, 2022, 10:43am UTC](https://discourse.julialang.org/t/get-the-output-of-a-julia-function-called-from-c/90699/1 "2022-11-23T10:43:47Z")

</div>

I have a Julia module called MyGramSchmidt which contains a function called fwht that takes a Vector{Float64} as input and returns a Vector{Float64}. I would like to define a C variable and use it to get the output of fwht called from C. I tried the following

```julia
#include <julia.h>
JULIA_DEFINE_FAST_TLS

int main(int argc, char *argv[]) {
  jl_init();
  jl_eval_string("using MyGramSchmidt: fwht");
  double z[8];
  z = jl_eval_string("fwht(rand(8))");
  jl_atexit_hook(0);
  return 0;
}

```

However, I get an “error: assignment to expression with array type” when I compile the C code. I wonder what is the proper way to do what I am trying to do.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [November 23, 2022, 11:10am UTC](https://discourse.julialang.org/t/get-the-output-of-a-julia-function-called-from-c/90699/2 "2022-11-23T11:10:06Z")

</div>

[https://docs.julialang.org/en/v1/manual/embedding/#Working-with-Arrays](https://docs.julialang.org/en/v1/manual/embedding/#Working-with-Arrays)  
In particular

> In order to access the data of `x`, we can use `jl_array_data`:
> 
> ```julia
> double *xData = (double*)jl_array_data(x);
> 
> ```

---

<div class="post-metadata">

### Author: ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)
#### Post date: [November 23, 2022, 12:32pm UTC](https://discourse.julialang.org/t/get-the-output-of-a-julia-function-called-from-c/90699/3 "2022-11-23T12:32:25Z")

</div>

Firstly, `jl_eval_string` returns `jl_value_t*`, which is a pointer to an arbitrary Julia value. To manipulate that value, you need to cast the value to the C representation of the data. In this case, it’s `jl_array_data`.

What’s more, you need to root that return value. The return value of `jl_eval_string` is by default not assigned to any Julia variable. Then the next time when GC happens, it will be reclaimed, which can lead to memory fault if you still hold and use this return value. You can choose to instead evaluate `ans = ...` or use JL\_GC\_PUSH and JL\_GC\_POP to root the value.

---

<div class="post-metadata">

### Author: ![nvenkov1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvenkov1/32/43679_2.png) [@nvenkov1](https://discourse.julialang.org/u/nvenkov1)
#### Post date: [November 23, 2022, 1:36pm UTC](https://discourse.julialang.org/t/get-the-output-of-a-julia-function-called-from-c/90699/4 "2022-11-23T13:36:02Z")

</div>

I am now doing the following:

```julia
#include <julia.h>
#include <stdio.h>
JULIA_DEFINE_FAST_TLS

int main(int argc, char *argv[]) {
  jl_init();
  jl_eval_string("using MyGramSchmidt: fwht");
  jl_array_t *z = (jl_array_t*)jl_eval_string("fwht(rand(8))");
  double *z_data = (double*)jl_array_data(z);
  printf("%g\n", z_data[0]);
  free(z_data);
  jl_atexit_hook(0);
  return 0;
}

```

This compiles correctly, but I get a segmentation fault when executing the code.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [November 23, 2022, 1:52pm UTC](https://discourse.julialang.org/t/get-the-output-of-a-julia-function-called-from-c/90699/5 "2022-11-23T13:52:07Z")

</div>

First of all, I recommend doing something simple which doesn’t involve your packages no one else has access to and can’t help you with, so for example do

```julia
  jl_array_t *z = (jl_array_t*)jl_eval_string("rand(8)");

```

instead of

```julia
  jl_eval_string("using MyGramSchmidt: fwht");
  z = jl_eval_string("fwht(rand(8))");

```

Then, you should really read the documentation carefully before diving into this: [Embedding Julia · The Julia Language](https://docs.julialang.org/en/v1/manual/embedding/#Memory-Management). @anon56330260 already outlined above what you have to do.

---

<div class="post-metadata">

### Author: ![nvenkov1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvenkov1/32/43679_2.png) [@nvenkov1](https://discourse.julialang.org/u/nvenkov1)
#### Post date: [November 23, 2022, 2:05pm UTC](https://discourse.julialang.org/t/get-the-output-of-a-julia-function-called-from-c/90699/6 "2022-11-23T14:05:12Z")

</div>

Okay, so let’s use this following example:

```julia
#include <julia.h>
#include <stdio.h>
JULIA_DEFINE_FAST_TLS
int main(int argc, char *argv[]) {
  jl_init();
  jl_array_t *z = (jl_array_t*)jl_eval_string("rand(8)");
  JL_GC_PUSH1(&z);
  double *z_data = (double*)jl_array_data(z);
  JL_GC_POP();
  printf("%g\n", z_data[0]);
  free(z_data);
  jl_atexit_hook(0);
  return 0;
}

```

I read the documentation, which is clear for how to proceed with scalar returned values, but all it says about accessing returned arrays is to cast the returned value as a jl\_array\_t pointer, which I do. I also follow what @anon56330260 said by using JL\_GC\_PUSH1 before I try to access the data, and JL\_GC\_POP after extracting the data with jl\_array\_data. I don’t understand what I am doing wrong.

---

<div class="post-metadata">

### Author: ![nvenkov1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvenkov1/32/43679_2.png) [@nvenkov1](https://discourse.julialang.org/u/nvenkov1)
#### Post date: [November 23, 2022, 2:08pm UTC](https://discourse.julialang.org/t/get-the-output-of-a-julia-function-called-from-c/90699/7 "2022-11-23T14:08:08Z")

</div>

Okay, the following works:

```julia
#include <julia.h>
#include <stdio.h>
JULIA_DEFINE_FAST_TLS
int main(int argc, char *argv[]) {
  jl_init();
  jl_array_t *z = (jl_array_t*)jl_eval_string("rand(8)");
  double *z_data = (double*)jl_array_data(z);
  printf("%g\n", z_data[0]);
  jl_atexit_hook(0);
  return 0;
}

```
