# Using ccall and pointer to pointer in linked list

**URL:** <https://discourse.julialang.org/t/using-ccall-and-pointer-to-pointer-in-linked-list/72041>\
**Category:** General Usage\
**Tags:** question, ccall, c\
**Created:** [November 25, 2021, 4:07am UTC](https://discourse.julialang.org/t/using-ccall-and-pointer-to-pointer-in-linked-list/72041 "2021-11-25T04:07:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)\
**Post date:** [November 25, 2021, 4:07am UTC](https://discourse.julialang.org/t/using-ccall-and-pointer-to-pointer-in-linked-list/72041/1 "2021-11-25T04:07:06Z")

</div>

Below is an example from K.N.King “C Programming” for a linked list, where `add_to_list()` acts pretty much like `pushfirst!()` of Julia:

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

struct node {
    int value;
    struct node *next;
};

void add_to_list(struct node **list, int n) {
    struct node *new_node;
    new_node = malloc(sizeof(struct node));
    new_node->value = n;
    new_node->next = *list;
    *list = new_node;
}

int main() {
    struct node *list = NULL;
    add_to_list(&list, 10);
    add_to_list(&list, 20);

    printf("First value: %d\n", list->value); // print 20
    return 1;
}

```

To call this from Julia via `ccall`, I did the following:

```julia
const LIBLIST = joinpath(@ __DIR__ , "liblist")
ccall((:main, LIBLIST), Cint, ()) # works fine!

mutable struct Node 
    value :: Cint
    next :: Ptr{Node}
end

list = C_NULL
ccall((:add_to_list, LIBLIST), Cvoid, (Ptr{Ptr{Node}}, Cint), list, 10)
ccall((:add_to_list, LIBLIST), Cvoid, (Ptr{Ptr{Node}}, Cint), list, 20)

println("done")

```

which does not work with a segmentation fault error.

In Julia, how should I initialize `list` and how should I pass to `add_to_list()`?

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [November 25, 2021, 5:47am UTC](https://discourse.julialang.org/t/using-ccall-and-pointer-to-pointer-in-linked-list/72041/2 "2021-11-25T05:47:10Z")

</div>

Your julia `list` should not be `C_NULL`. Your C-code will dereference `NULL` value.

> [@chkwon](#):
>
> ```julia
> new_node->next = *list;
> *list = new_node;
> 
> ```

---

<div class="post-metadata">

**Author:** ![chkwon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chkwon/32/3632_2.png) [@chkwon](https://discourse.julialang.org/u/chkwon)\
**Post date:** [November 25, 2021, 6:55am UTC](https://discourse.julialang.org/t/using-ccall-and-pointer-to-pointer-in-linked-list/72041/3 "2021-11-25T06:55:39Z")

</div>

Thanks, @tomaklutfu. Indeed it shouldn’t be NULL.

The following worked:

```julia

list_ref = Ref{Ptr{Node}}()

ccall((:add_to_list, LIBLIST), Cvoid, (Ptr{Ptr{Node}}, Cint), list_ref, 10)
ccall((:add_to_list, LIBLIST), Cvoid, (Ptr{Ptr{Node}}, Cint), list_ref, 20)

list = unsafe_load(list_ref[])
@show list.value # 20

```

[This comment](https://discourse.julialang.org/t/what-is-the-correct-way-to-create-a-pointer-to-pointer/71442/3) was helpful.
