Does ANSI prohibit assignments between overlapping union components?

brnstnd at stealth.acf.nyu.edu brnstnd at stealth.acf.nyu.edu
Sat Feb 17 14:07:22 AEST 1990


Try this program on your favorite compiler:

  #include <stdio.h>
  struct str { char c[20]; } ;
  static struct str test;
  static union { struct { void *v; struct str s; } c; struct str s; } u;
  static char c = '\0'; /* don't-crash kludge */
  main()
  {
   int i; for (i = 0;i < 15;i++) test.c[i] = 'a' + i;
   u.s = test; puts(u.s.c); 
   u.c.s = u.s; puts(u.c.s.c);
   u.s = u.c.s; puts(u.s.c);
  }

It looks valid to me: after u.s = test, u.s is active, so accessing u.s
is fine; then after u.c.s = u.s, u.c.s is active, so accessing u.c.s is
fine; and so on. Is there an ANSI rule prohibiting this? If not, quite a
few supposedly conformant compilers are broken.

---Dan



More information about the Comp.lang.c mailing list