// filename:c2011-6-5-2-3-ex.c // original examples and/or notes: // (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011 // http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf // C2011 6.5.2.3 Structure and union members // compile and output mechanism: // (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.xx, 2013 // compile errors and/or wornings: // 1 (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn) // Target: x86_64-apple-darwin11.4.2 //Thread model: posix // (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign. // 2 gcc-4.9 (GCC) 4.9.0 20131229 (experimental) // Copyright (C) 2013 Free Software Foundation, Inc. #include #include //Example1 struct e1 { int x;}; struct e1 fu(void){ struct e1 ef; return ef; } //Example3 union { struct { int alltypes; } n; struct { int type; int intnode; } ni; struct { int type; double doublenode; } nf; } u; void h(void){// add for compilation u.nf.type = 1; u.nf.doublenode = 3.14; /* ... */ if (u.n.alltypes == 1) if (sin(u.nf.doublenode) == 0.0) ; printf("%d %f\n", u.n.alltypes,sin(u.nf.doublenode)); }// add for compilation /* ... */ struct t1 { int m; }; struct t2 { int m; }; int f(struct t1 *p1, struct t2 *p2){ if (p1->m < 0) p2->m = -p2->m; return p1->m; } int g(){ union { struct t1 s1; struct t2 s2; } u; /* ... */ return printf("g()=%d ",f(&u.s1, &u.s2)); } // Example2 struct s { int i; const int ci; }; struct s s; const struct s cs; volatile struct s vs; int main(void){ h(); g(); // for Example 2 printf("%d ",s.i); //int printf("%d ",s.ci); //const int printf("%d ",cs.i); //const int printf("%d ",cs.ci); //const int printf("%d ",vs.i); //volatile int printf("%d ",vs.ci); //volatile const int // for Example 1 return printf("\n6.5.2.3 Structure and union members %d\n", fu().x); } // 1. output LLVM 3.2 // 1 0.001593 // g()=0 0 0 0 0 0 0 // 6.5.2.3 Structure and union members 0 // 2. output GCC 4.0 // 1 0.001593 // g()=1383103152 0 0 0 0 0 0 // 6.5.2.3 Structure and union members 0