# Zero-sized structs in C types

**URL:** https://internals.rust-lang.org/t/zero-sized-structs-in-c-types/21307
**Category:** Unsafe Code Guidelines
**Created:** [August 2, 2024, 3:40pm UTC](https://internals.rust-lang.org/t/zero-sized-structs-in-c-types/21307 "2024-08-02T15:40:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![d3zd3z](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/d3zd3z/32/511_2.png) [@d3zd3z](https://internals.rust-lang.org/u/d3zd3z)
#### Post date: [August 2, 2024, 3:40pm UTC](https://internals.rust-lang.org/t/zero-sized-structs-in-c-types/21307/1 "2024-08-02T15:40:51Z")

</div>

Currently, rustc warns if structures passed to C contain zero sized elements. I am building representations of structs (using bindgen) for running Rust on Zephyr, and have found a few structs, that depending on the configuration, end up as zero sized. It seems that both gcc and clang support this. Given that two significant toolchains support zero-sized structs in C code, what would be the possibility of having a way to suppress this warning (without suppressing other reasons for things to be c-unsafe). Or have I missed something, and there is already a way to suppress this particular warning.

Currently, I've enabled a workaround that is used for C++, which doesn't allow the zero-sized structs, but it'd be nice to avoid the wasted space of dummy elements defined by the workaround.

---

<div class="post-metadata">

### Author: ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)
#### Post date: [August 2, 2024, 4:28pm UTC](https://internals.rust-lang.org/t/zero-sized-structs-in-c-types/21307/2 "2024-08-02T16:28:09Z")

</div>

The problem is that different C compilers do different things with zero-sized structs. The warning is there to tell you that the layout you are getting in Rust might not be matching the layout that you are getting in C.

Also see [repr(C) is unsound on MSVC targets · Issue #81996 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/81996), in particular the [summary here](https://github.com/rust-lang/rust/issues/81996#issuecomment-805340241).

---

<div class="post-metadata">

### Author: ![d3zd3z](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/d3zd3z/32/511_2.png) [@d3zd3z](https://internals.rust-lang.org/u/d3zd3z)
#### Post date: [August 5, 2024, 2:14pm UTC](https://internals.rust-lang.org/t/zero-sized-structs-in-c-types/21307/3 "2024-08-05T14:14:02Z")

</div>

Is there a way to suppress this warning, ideally just this one, and not the whole C-FFI warnings? In my specific case, I do know what my compiler is doing, and bindgen even generates code to verify that the behavior is the same.

---

<div class="post-metadata">

### Author: ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)
#### Post date: [August 5, 2024, 3:29pm UTC](https://internals.rust-lang.org/t/zero-sized-structs-in-c-types/21307/4 "2024-08-05T15:29:28Z")

</div>

Have you tried `#[allow()]`ing it on just the types and/or functions in question?

---

<div class="post-metadata">

### Author: ![d3zd3z](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/d3zd3z/32/511_2.png) [@d3zd3z](https://internals.rust-lang.org/u/d3zd3z)
#### Post date: [March 21, 2025, 10:35pm UTC](https://internals.rust-lang.org/t/zero-sized-structs-in-c-types/21307/5 "2025-03-21T22:35:10Z")

</div>

So, the problem here is that bindgen doesn't (currently) provide any way to add annotations to selected types. In my case, I just allowed it for the entire bindgen-generated output, which is likely to be ok, given that they come from C declarations.

---

<div class="post-metadata">

### Author: ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)
#### Post date: [September 12, 2026, 10:35pm UTC](https://internals.rust-lang.org/t/zero-sized-structs-in-c-types/21307/6 "2026-09-12T22:35:42Z")

</div>

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
