# Passing an array of structures through \`ccall\`

**URL:** <https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 2, 2017, 9:35pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194 "2017-08-02T21:35:16Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [August 2, 2017, 9:35pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/1 "2017-08-02T21:35:16Z")

</div>

I’m trying to pass an array of structure (actually, a pointer to the chunk of memory containing the first structure) from Julia to C through `ccall`.

An example of what I’m trying to do is shown below, using just C:

```julia
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

//Define a test structure
struct st_test {
    int i;
    double d;
    char s[10];
    double a[2];
    int b[2];
};

//Print the contents of an array of structure, and modify their values
//upon return.
int printAndModify(struct st_test* pointer, int size) {
    int i;
    printf("\nin C:\n");

    //Loop through elements in the array
    for (i=0; i<size; i++) {

        //Pointer to current element
        struct st_test* cur = &(pointer[i]);

        //Print structure contents
        printf("index %d/%d, memory address %p\n", i+1, size, cur);
        printf(" i=%d\n d=%f\n s=%10s\n a=%f %f\n b=%d %d\n",
               cur->i, cur->d, cur->s,
               cur->a[0], cur->a[1], cur->b[0], cur->b[1]);

        //Modify structure contents
        cur->i = 100 + i;
        cur->d += 100;
        strcpy(cur->s, "from C ");
        cur->a[0] *= 3.;
        cur->a[1] *= 4.;
        cur->b[0] *= 5.;
        cur->b[1] *= 6.;
    }
    return 1;
}

void test_c() {
    //Allocate memory for the array of structures. NOTE: all
    //structures are stored in CONTIGUOUS chunks!
    const int size = 2;
    struct st_test* array = malloc(sizeof(struct st_test) * size);

    //Fill values
    int i;
    for (i=0; i<size; i++) {
        //Pointer to current structure
        struct st_test* cur = &(array[i]);

        cur->i = 1;
        cur->d = 2;
        strcpy(cur->s, "from test_c");
        cur->a[0] = 3;
        cur->a[1] = 4;
        cur->b[0] = 5;
        cur->b[1] = 6;
    }

    printAndModify(array, size);
    free(array);
}

```

After compiling the above code as a shared library, I wanted to implement the `test_c` routine in Julia, as follows:

```julia
#Define a julia structure corresponding to the C one
mutable struct st_test
  i::Cint
  d::Cdouble
  s::NTuple{10, Cchar}
  a::NTuple{2, Cdouble}
  b::NTuple{2, Cint}
end

#Simple constructor for the structure 
st_test() = st_test(1, 2., NTuple{10,Cchar}("from Julia"), (3.,4.), (5, 6))

#Create an array of structures
array = [st_test() for i=1:2]

#Size of the array
size = Cint(length(array))

ret = ccall((:printAndModify, "./libarrayIssue.so"), Cint, (Ptr{st_test}, Cint), 
            array, size)

#Print structure contents
@printf("\nin Julia:\n");
for i in 1:size
    cur = array[i]
    @printf("index %d/%d\n", i, size);
    @printf(" i=%d\n d=%f\n s=%10s\n a=%f %f\n b=%d %d\n",
		   cur.i, cur.d, join(Char.(cur.s), ""),
		   cur.a[1], cur.a[2], cur.b[1], cur.b[2]);
end

```

However, this Julia code ends up in a segfault since (as far as I understood) the structures passed through `ccall` are not allocated in **contiguous** chunks of memory, while in C they were one next to the other.

The only way I found to solve the problem is to create a new version of the `printAndModify` routine, called `printAndModify_new`:

```julia
int printAndModify_new(struct st_test** pointer, int size) {
    // CHANGED HERE ^
    int i;
    printf("\nin C:\n");

    //Loop through elements in the array
    for (i=0; i<size; i++) {

        //Pointer to current element
        struct st_test* cur = pointer[i]; // <-- CHANGED HERE

        //Print structure contents
        printf("index %d/%d, memory address %p\n", i+1, size, cur);
        printf(" i=%d\n d=%f\n s=%10s\n a=%f %f\n b=%d %d\n",
               cur->i, cur->d, cur->s,
               cur->a[0], cur->a[1], cur->b[0], cur->b[1]);

        //Modify structure contents
        cur->i = 100 + i;
        cur->d += 100;
        strcpy(cur->s, "from C ");
        cur->a[0] *= 3.;
        cur->a[1] *= 4.;
        cur->b[0] *= 5.;
        cur->b[1] *= 6.;
    }
    return 1;
}

```

which I call from Julia with:

```julia
ret = ccall((:printAndModify_new, "./libarrayIssue.so"), Cint, (Ptr{st_test}, Cint), 
            array, size)

```

Notice the change in the input parameter: now it is a `st_test**`.

This is a significantly simplified version of a problem I’m having in calling a C library, and I’m not going to change all the `st_test*` to `st_test**` in the code.

Is there anyway I can pass an `Array{st_test,1}` through `ccall` retaining the original function definition with `st_test*`, i.e. by having the structures stored in continguous chunks of memory?

---

<div class="post-metadata">

**Author:** ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)\
**Post date:** [August 2, 2017, 10:37pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/2 "2017-08-02T22:37:30Z")

</div>

I think you can get what you want if `st_test` is an immutable struct in Julia.

---

<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:** [August 2, 2017, 10:39pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/3 "2017-08-02T22:39:33Z")

</div>

As someone took my answer 5 seconds before I was gonna post it :P, just want to confirm that using a normal `struct` works:

```julia
index 1/2
  i=100
  d=102.000000
  s= from C   
  a=9.000000 16.000000
  b=25 36
index 2/2
  i=101
  d=102.000000
  s= from C   
  a=9.000000 16.000000
  b=25 36

```

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [August 2, 2017, 10:45pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/4 "2017-08-02T22:45:53Z")

</div>

Sorry, I forgot to mention… I need `st_test` to be mutable, i.e. I need to do something like ` array[1].i = 4`.  
So the (updated) question is now:

Is there anyway I can pass an `Array{st_test,1}` through `ccall` retaining the original function definition with `st_test*` (i.e. by having the structures stored in continguous chunks of memory) _and having `st_test` declared as mutable_ ?

---

<div class="post-metadata">

**Author:** ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)\
**Post date:** [August 2, 2017, 10:56pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/5 "2017-08-02T22:56:36Z")

</div>

I don’t believe it is possible to have `st_test` mutable and have it stored contiguous in memory in an array.

But is mutability of the array elements actually important? Or is it ok to do something like the following when you need to update an element of the list:

```julia
old_element = array[1]
new_element = st_test(4, old_element.d, old_element.s, old_element.a, old_element.b)
array[1] = new_element

```

You can hide this messiness behind a helper function for now, but I believe there is syntax on the horizon that will make this less annoying

[https://github.com/JuliaLang/julia/pull/21912](https://github.com/JuliaLang/julia/pull/21912)

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [August 2, 2017, 11:44pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/6 "2017-08-02T23:44:43Z")

</div>

Well, this is going to be quite tricky, while the new syntax may definitely be a solution.

Until the question is settled, I will resort to simply copying the data into contiguous chunks, and then back to Julia’s array, using the following C code:

```julia
void* copyArrayData(void** src, ssize_t size, ssize_t len) {
    void* out = malloc(size * len);
    void* dst = out;

    ssize_t i;
    for (i=0; i<len; i++) {
        printf("Copying data: %p -> %p\n", src[i], dst);
        memcpy(dst, src[i], size);
        dst += size;
    }

    return out;
}

void restoreArrayData(void* src, void** dst, ssize_t size, ssize_t len) {
    void* toBeFreed = src;
    ssize_t i;
    for (i=0; i<len; i++) {
        printf("Copying data: %p <- %p\n", dst[i], src);
        memcpy(dst[i], src, size);
        src += size;
    }
    free(toBeFreed);
}

```

The Julia code will be:

```julia
tmp = ccall((:copyArrayData, "./libarrayIssue.so"), Ptr{Void}, (Ptr{Void}, Csize_t, Csize_t), 
            array, sizeof(st_test), size)

ret = ccall((:printAndModify, "./libarrayIssue.so"), Cint, (Ptr{st_test}, Cint), 
            tmp, size)

ccall((:restoreArrayData, "./libarrayIssue.so"), Void, (Ptr{Void}, Ptr{Void}, Csize_t, Csize_t), 
      tmp, array, sizeof(st_test), size)

```

This is far from being elegant and has poor performances, but is currently the best solution (IMHO).

Do you think these routines should be generally available in Julia?  
I believe my problem is quite common among people who wish to use C libraries from Julia, and having them well documented could save hours of struggling…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 3, 2017, 12:04am UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/7 "2017-08-03T00:04:37Z")

</div>

> [@gcalderone](#):
>
> Do you think these routines should be generally available in Julia?

No. the correct solution should.

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [August 3, 2017, 12:07am UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/8 "2017-08-03T00:07:12Z")

</div>

Agree!  
…but what is the correct solution ? 🤔

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 3, 2017, 12:35am UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/9 "2017-08-03T00:35:30Z")

</div>

> [@Passing an array of structures through \`ccall\`](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/5):
>
> I don’t believe it is possible to have st\_test mutable and have it stored contiguous in memory in an array. But is mutability of the array elements actually important? Or is it ok to do something like the following when you need to update an element of the list: old\_element = array[1] new\_element = st\_test(4, old\_element.d, old\_element.s, old\_element.a, old\_element.b) array[1] = new\_element You can hide this messiness behind a helper function for now, but I believe there is syntax on the hori…

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [August 3, 2017, 9:59am UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/10 "2017-08-03T09:59:50Z")

</div>

Sorry, I’m a bit new to Julia and maybe I miss the point…

The following code ultimately results in some sort of copy between memory locations, isn’t it?

> [@Michael\_Eastwood](#):
>
> old\_element = array[1]  
> new\_element = st\_test(4, old\_element.d, old\_element.s, old\_element.a, old\_element.b)  
> array[1] = new\_element

Also the new syntax (`immutable_struct@field = value`) is just syntactic sugar to perform the copy proposed by @Michael_Eastwood, right?

Moreover those methods are either tricky (Julia is supposed to be a high level language…) or are not yet implemented. Then why these approaches are better than the one based on C’s `memcpy` ?

Clearly there are performance tradeoffs related to how many times the array is modified and how large is the structure, which makes one of the above methods preferable over the others, but there is no clear winner a-priori.

Moreover the performance flaw is given by our will to call C code which (at design time) was not supposed to be called by Julia, not by Julia itself.

Finally, the `copyArrayData` / `restoreArrayData` method implemented above is:

- completely generic (based on `void*` pointers);
- very easy to implement and to discuss in the documentation;
- specifically targeted to solve the problem of calling C/Fortran code which requires the structures to be stored contiguously in memory.

One final question: are we sure that in all future Julia implementations an `Array` of immutable structures will always be stored as contiguous chunks in memory?  
If not, the `memcpy` approach proposed here is the only way to go…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 3, 2017, 11:20am UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/11 "2017-08-03T11:20:24Z")

</div>

> [@gcalderone](#):
>
> The following code ultimately results in some sort of copy between memory locations, isn’t it?

Yes

> [@gcalderone](#):
>
> Also the new syntax (immutable\_struct@field = value) is just syntactic sugar to perform the copy proposed by @Michael_Eastwood, right?

No it mustn’t.

> [@gcalderone](#):
>
> Then why these approaches are better than the one based on C’s memcpy ?

I don’t see why not yet implemented means worse. I didn’t say you couldn’t use the less efficient one, just that we won’t have it in Base and will never recommend it.

> [@gcalderone](#):
>
> completely generic (based on void\* pointers);

No, it can miss write barriers for the really generic case.

> [@gcalderone](#):
>
> very easy to implement and to discuss in the documentation;

Sure, doesn’t make it good.

> [@gcalderone](#):
>
> specifically targeted to solve the problem of calling C/Fortran code which requires the structures to be stored contiguously in memory.

And this doesn’t make it the correct solution either.

> [@gcalderone](#):
>
> If not, the memcpy approach proposed here is the only way to go…

No that’s exactly the case using `memcpy` is wrong.

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [August 3, 2017, 11:50am UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/12 "2017-08-03T11:50:35Z")

</div>

Wow, it looks like I have a lot to learn… 😉

By the way:

> [@yuyichao](#):
>
> I don’t see why not yet implemented means worse.

well, this depends on the point of view: You (I argue) are a Julia developer, I’m a user.

> [@yuyichao](#):
>
> it can miss write barriers for the really generic case.

Currently any user can easily produce strange or unwanted behaviors, even segfaults, using `ccall`. Users know it is something to be used with great care.

However what you mentioned is exactly the reason why a Julia implementation would be desirable: it **could** help users avoid doing the wrong thing.

I’ll considered the question closed, the answer being three-fold:

- implement the solution kindly suggested by @Michael_Eastwood;
- wait for the new syntax to be implemented;
- implement a simple C code which does the hard work and take your risks.

I’ll go for the third… 😉

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 3, 2017, 12:04pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/13 "2017-08-03T12:04:55Z")

</div>

> [@gcalderone](#):
>
> well, this depends on the point of view: You (I argue) are a Julia developer, I’m a user.

I mean that certainly mean you can’t use it for now, but it doesn’t make it a worse long term solution in base.

> [@gcalderone](#):
>
> Currently any user can easily produce strange or unwanted behaviors, even segfaults, using ccall. Users know it is something to be used with great care.

No, not when used correctly.

> [@gcalderone](#):
>
> However what you mentioned is exactly the reason why a Julia implementation would be desirable: it could help users avoid doing the wrong thing.

That’s why we want a correct julia implimentation. FWIW, for non-inlined immutable, it is impossible to implement a version without the C code being aware of julia.

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [August 9, 2017, 3:42pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/14 "2017-08-09T15:42:34Z")

</div>

A small update:

I implemented a (hopefully useful and general enough) solution following @Michael_Eastwood suggestion which works as follows: the user define a `mutable` structure, then calls a function (named `defineImmutableStruct`, see below) which automatically defines:

- a similar (same fields and types) **immutable** structure, with name prefix `immutable_`;
- an inner constructor for the immutable structure accepting as input an instance of the mutable one;
- an outer constructor for the mutable structure accepting as input an instance of the immutable one;

The code is:

```julia
function defineImmutableStruct(stype)
    fsymb = fieldnames(stype)
    fname = convert.(String, fsymb)
    ftype = Vector{String}()
    map(fieldtype.(stype, fsymb)) do x
        push!(ftype, @sprintf "%s" x)
    end

    name = @sprintf "%s" stype

    # Get rid of module prefix (if any)
    name = split(name, ".")
    name = String(name[end])

    out = fname .* "::" .* ftype
    out = "struct immutable_" * name * "\n" * join(" " .* fname .* "::" .* ftype, "\n")
    out *= "\n\n"
    out *= " immutable_" * name * "(p::" * name * ") = new(" * join("p." .* fname, ", ") * ")"
    out *= "\n"
    out *= "end"
    out *= "\n"
    #print(out)
    eval(parse(out))

    out = name * "(p::immutable_" * name * ") = " * name * "(" * join("p." .* fname, ", ") * ")\n"
    #print(out)
    eval(parse(out))
end

# Define a mutable structure
mutable struct st_test
  i::Cint
  d::Cdouble
  s::NTuple{10, Cchar}
  a::NTuple{2, Cdouble}
  b::NTuple{2, Cint}
end

#Simple constructor for the structure
st_test() = st_test(1, 2., NTuple{10,Cchar}("from Julia"), (3.,4.), (5, 6))

# Define the corresponding immutable structure and the constructors to switch back and forth:
defineImmutableStruct(st_test)

```

A call to `whos()` shows that we have two structure definitions (clearly with the same size):

```julia
    immutable_st_test 160 bytes DataType
              st_test 160 bytes DataType

```

Now let’s create an array of **mutable** structures, fill the values and convert it to an array of **immutable** ones:

```julia
array = [st_test() for i=1:2]
array[1].i = 10
array[2].i = 20
immutable_array = map(immutable_st_test, array)

```

We can safely pass the `immutable_array` through `ccall` since the structures are now stored in contiguous chunks. Upon return we can copy the (possibly modified) data from the `immutable_array` into an array of mutable structures:

```julia
new_array = map(st_test, immutable_array)

```

This solution is still not-so-elegant as I hoped, but is quick, effective and do not require C code. Also, the performances are similar to the `copyArrayData` / `restoreArrayData` approach described above.

For a better solution we should wait for the `(immutable_struct@field = value)` syntax…

---

<div class="post-metadata">

**Author:** ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)\
**Post date:** [August 9, 2017, 5:20pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/15 "2017-08-09T17:20:49Z")

</div>

Oo, this is the perfect opportunity to bust out a macro. The following macro defines a mutable struct to pair with all of your immutable structs. I think this solution is a little cleaner

```julia
julia> macro define_mutable(immutable_struct)
           immutable_name = immutable_struct.args[2]
           mutable_name = Symbol(immutable_name, "_mutable")

           mutable_struct = copy(immutable_struct)
           mutable_struct.args[1] = true # set the mutability to true
           mutable_struct.args[2] = mutable_name

           constructors = quote
               function $mutable_name(x::$immutable_name)
                   $mutable_name(ntuple(i->getfield(x, i), nfields($immutable_name))...)
               end
               function $immutable_name(x::$mutable_name)
                   $immutable_name(ntuple(i->getfield(x, i), nfields($mutable_name))...)
               end
           end

           output = Expr(:block, immutable_struct, mutable_struct, constructors)
           esc(output)
       end
@define_mutable (macro with 1 method)

julia> @define_mutable struct Foo
           bar::Int
           baz::Float64
           barbaz
       end
Foo

julia> foo = Foo(1, 1.0, "hello")
Foo(1, 1.0, "hello")

julia> foo_mutable = Foo_mutable(foo)
Foo_mutable(1, 1.0, "hello")

julia> foo_mutable.barbaz = π
π = 3.1415926535897...

julia> Foo(foo_mutable)
Foo(1, 1.0, π = 3.1415926535897...)

```

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [August 9, 2017, 8:30pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/16 "2017-08-09T20:30:50Z")

</div>

Great!!  
This is much better, thank you very much!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 14, 2017, 3:00pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/17 "2017-08-14T15:00:37Z")

</div>

I should perhaps point out why incomplete but usable implementations of useful things cannot generally go into the language’s standard library: if we have it, then it must work in _all_ circumstances. Once we’ve added a feature – especially once 1.0 comes out, which we’re working day and night towards – then it must be supported for the duration (which may be 5-10 years). If a user-defined work-around (hack, macro, whatever) happens to work for your use case, great, by all means use it! But features in the language don’t have the luxury of cherry-picking use cases that happen to work – they need to support all possible use cases.

---

<div class="post-metadata">

**Author:** ![ColinCaine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colincaine/32/7211_2.png) [@ColinCaine](https://discourse.julialang.org/u/ColinCaine)\
**Post date:** [November 24, 2021, 8:23pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194/18 "2021-11-24T20:23:33Z")

</div>

This code should use `fieldcount` rather than `nfields` otherwise you’re getting the number of fields in `DataType`, not in your struct.

We got caught by this (see also [https://github.com/JuliaLang/julia/issues/29977](https://github.com/JuliaLang/julia/issues/29977))
