# Wrapping C function returning struct containing character array (Cstring)

**URL:** <https://discourse.julialang.org/t/wrapping-c-function-returning-struct-containing-character-array-cstring/101574>\
**Category:** New to Julia\
**Created:** [July 13, 2023, 1:16pm UTC](https://discourse.julialang.org/t/wrapping-c-function-returning-struct-containing-character-array-cstring/101574 "2023-07-13T13:16:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [July 13, 2023, 1:16pm UTC](https://discourse.julialang.org/t/wrapping-c-function-returning-struct-containing-character-array-cstring/101574/1 "2023-07-13T13:16:51Z")

</div>

Example code, but essentially what the C library that I am wrapping returns:

```plaintext
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define BUFSIZE 128
struct C {
    int a;
    double b;
    char c[BUFSIZE];
};

struct C returns_struct() {
    struct C retval;
    retval.a = 1;
    retval.b = 2.2;
    strncpy(retval.c, "hi there", BUFSIZE);
    return retval;
}

int main(int argc, char *argv) {
    struct C thing = returns_struct();
    printf("thing.a = %d\n", thing.a);
    printf("thing.b = %f\n", thing.b);
    printf("thing.c = %s\n", thing.c);
    return EXIT_SUCCESS;
}

```

Although it has a main function (for testing), I compile it with `gcc -fPIC -shared stuff.c -o libstuff.so`. In Julia I try to wrap it with:

```julia
struct C
    a::Cint
    b::Cdouble
    c::Cstring # <-- the problem appears to be this
end

stuff = @ccall "./libstuff.so".returns_struct()::C

```

If I remove the string from the struct, things seem to work well … any idea what I am missing?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 13, 2023, 2:28pm UTC](https://discourse.julialang.org/t/wrapping-c-function-returning-struct-containing-character-array-cstring/101574/2 "2023-07-13T14:28:19Z")

</div>

`Cstring` is essentially a pointer whereas your `C` struct contains a fixed size array. Try with

```julia
struct C
    a::Cint
    b::Cdouble
    c::NTuple{128, Cchar}
end
```

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [July 13, 2023, 2:57pm UTC](https://discourse.julialang.org/t/wrapping-c-function-returning-struct-containing-character-array-cstring/101574/3 "2023-07-13T14:57:29Z")

</div>

```julia
stuff = @ccall "./libstuff.so".returns_struct()::C
C(1, 2.2, (104, 105, 32, 116, 104, 101, 114, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))

```

… I think I can work with that, thanks!  
I can use this clunky expression to get the Julia representation of it:

```julia
join(convert.(Char, filter(x -> x > 0, stuff.c)))

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 13, 2023, 3:17pm UTC](https://discourse.julialang.org/t/wrapping-c-function-returning-struct-containing-character-array-cstring/101574/4 "2023-07-13T15:17:50Z")

</div>

There’s probably a better solution but this is at least less clunky:

```julia
String(UInt8[c for c in stuff.c])
```
