# Binding C union in Fortran

**URL:** <https://fortran-lang.discourse.group/t/binding-c-union-in-fortran/9218>\
**Category:** Uncategorized\
**Created:** [February 15, 2025, 8:47am UTC](https://fortran-lang.discourse.group/t/binding-c-union-in-fortran/9218 "2025-02-15T08:47:20Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [February 15, 2025, 10:14am UTC](https://fortran-lang.discourse.group/t/binding-c-union-in-fortran/9218/2 "2025-02-15T10:14:06Z")

</div>

> [@MaximeC](#):
>
> or is there something else that could work to allow the bindings?

Something which I imagine would [work is](https://stackoverflow.com/a/42845034/4283055)

> 1. Hide the whole struct and expose accessor functions that act on a pointer

Unions fall outside of the interoperable entities, so the classic approach to solve this is using [indirection](https://en.wikipedia.org/wiki/Indirection) with an [opaque pointer](https://en.wikipedia.org/wiki/Opaque_pointer).

* * *

> [@MaximeC](#):
>
> I saw there was equivalence and transfer but it says this is deprecated, very surprisingly.

Neither are deprecated, however `equivalence` has been declared “obsolescent”. AFAICR, `equivalence` is limited to intrinsic numeric and character types, and sequence types, but not user-defined derived types.

I guess `transfer` could work, as long as you know the width of the union type. But it would still be a [loophole](https://fortran-lang.discourse.group/t/implicit-real-complex-conversion-in-fortran/7381/38):

> The loophole lets the programmer breach the type-checking by the compiler. It is a way to say: “Don’t interfere, as I am smarter than the rules”.

Also, welcome to Fortran Discourse!

---

_[View the full topic](https://fortran-lang.discourse.group/t/binding-c-union-in-fortran/9218)._
