# Dealing with complex C structure

**URL:** <https://discourse.julialang.org/t/dealing-with-complex-c-structure/9045>\
**Category:** New to Julia\
**Tags:** ccall\
**Created:** [February 14, 2018, 2:06am UTC](https://discourse.julialang.org/t/dealing-with-complex-c-structure/9045 "2018-02-14T02:06:53Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [February 14, 2018, 5:28am UTC](https://discourse.julialang.org/t/dealing-with-complex-c-structure/9045/2 "2018-02-14T05:28:57Z")

</div>

Taking the approach from the doc:

> You can get a near approximation of a union if you know, a priori, the field that will have the greatest size (potentially including padding). When translating your fields to Julia, declare the Julia field to be only of that type.

That’s not too bad. Here’s a MWE:

```C
/* test.c */
typedef struct abc
{
        union {
                unsigned int a;
                long b;
        } ab;
        int c;
} ABC, *ABCP;

ABC make1()
{
        ABC x;
        x.c = 1;
        x.ab.b = 2;
        return x;
}

ABC make2()
{
        ABC x;
        x.c = 1;
        x.ab.a = 2;
        return x;
}

```

Compile as usual:

```julia
$ gcc -fPIC -shared test.c -o libtest.so

```

In Julia, I can treat the union as a regular `long` variable (which is the largest in this example):

```julia
julia> const TEST = "/tmp/libtest.so"
"/tmp/libtest.so"

julia> x = ccall((:make1, TEST), ABC, ())
ABC(2, 1)

julia> y = ccall((:make2, TEST), ABC, ())
ABC(5815918277548834818, 1)

```

So the value returned by `make2` is obviously messed up because I should be looking for the `int` rather than the `long` field. To correct that, I can do some bit twiddling (not my favorite but some people here like it 🙂 )

```julia
julia> UInt64(y.ab)
0x50b64adc00000002

julia> Int32(y.ab & 0x00000000ffffffff)
2

```

I really need a way to reinterpret the thing so perhaps I just go with a fixed size tuple.

```julia
julia> struct ABC2
         ab::NTuple{8, Cuchar} # 8 bytes
         c::Int32
       end

julia> z = ccall((:make2, TEST), ABC2, ())
ABC2((0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00), 1)

julia> reinterpret(Int32, collect(z.ab[1:4]))
1-element Array{Int32,1}:
 2

```

Next, I’ll try to reinterpret to another struct. Hopefully that will work.

Does this all make sense? Is there any better way to approach this?

---

_[View the full topic](https://discourse.julialang.org/t/dealing-with-complex-c-structure/9045)._
