is it legal to take the address of an union member in C?

c, unions

Solution

Why shouldn't it be legal? Actually, it's even guaranteed that the addresses of every member is equal to the address of whole union:

A pointer to a union object, suitably converted, points to each of its members [...] and vice versa.

(C11, §6.7.2.1 16)

(which implies that you can take a pointer to union members)

Problem

I want to do something like this: ``` union U { int i; double d; }; void foo (double *d) { *d = 3.4; } int main () { union U u; foo (&(u.d)); } ``` `gcc` does not complain (with `-Wall -Wextra`) and it works as expected, but I would like to make sure it is actually legal (according to the standard).

Original source