# Using crystal with a C struct of undetermined size

**URL:** <https://forum.crystal-lang.org/t/using-crystal-with-a-c-struct-of-undetermined-size/3966>\
**Category:** Help & Support\
**Created:** [October 19, 2021, 1:33am UTC](https://forum.crystal-lang.org/t/using-crystal-with-a-c-struct-of-undetermined-size/3966 "2021-10-19T01:33:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric.johnson](https://avatars.discourse-cdn.com/v4/letter/e/77aa72/32.png) [@eric.johnson](https://forum.crystal-lang.org/u/eric.johnson)\
**Post date:** [October 19, 2021, 1:33am UTC](https://forum.crystal-lang.org/t/using-crystal-with-a-c-struct-of-undetermined-size/3966/1 "2021-10-19T01:33:40Z")

</div>

I’m brand new to Crystal. So far I really like what I see. I’ve been experimenting with Crystal’s ability to work with C code. One thing I haven’t been able to figure out so far is what the best way is to work with a C struct with a definition similar to this:

```auto
struct string_t {
  unsigned char n;
  char characters[]; 
};           

```

where `n` is the length of character data in the `characters` array.

I’m getting a pointer to a struct of `string_t` from a file, so the format is already defined, and the data is already populated.

I’ve tried this:

```auto
@[Extern]
struct ShortString
  @n = uninitialized UInt8
  @characters = uninitialized UInt8[0]
  getter :n
  def characters
    String.new(pointerof(@characters).as(UInt8*), @n)
  end
end

```

I feel like I’m close, but it doesn’t seem to do what I want. The value for @n seems to be populated correctly. However, the way I’m handling `characters` must be incorrect, as I’m not seeing the data I’d expect, but I haven’t been able to come up with an alternate formulation that seems to make sense and the compiler is ok with.

Any suggestions for the correct way to handle this?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [October 19, 2021, 1:49am UTC](https://forum.crystal-lang.org/t/using-crystal-with-a-c-struct-of-undetermined-size/3966/2 "2021-10-19T01:49:09Z")

</div>

[Binding to C flexible array members · Issue #10598 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/10598) might help.

---

<div class="post-metadata">

**Author:** ![eric.johnson](https://avatars.discourse-cdn.com/v4/letter/e/77aa72/32.png) [@eric.johnson](https://forum.crystal-lang.org/u/eric.johnson)\
**Post date:** [October 19, 2021, 1:57am UTC](https://forum.crystal-lang.org/t/using-crystal-with-a-c-struct-of-undetermined-size/3966/3 "2021-10-19T01:57:37Z")

</div>

this is perfect, thank you for the link!
