// filename:c2011-6-5-2-5-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.5 Compound literals // 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 struct int_list { int car; struct int_list *cdr; }; struct int_list endless_zeros = {0, &endless_zeros}; int eval (struct int_list l){ return printf(" %d ",l); } int *p = (int []){2, 4}; void f(void) { int *p; int *q = (int []){2, 4}; p=q; /*...*/ p = (int [2]){*p}; /*...*/ printf("%d ", *p); } struct point { int x; int y; }; int drawline (struct point i, struct point j){ return (i.x-j.x)^2+(i.y-j.y)^2; } int drawline2 (struct point* i, struct point* j){ return (i->x-j->x)^2+(i->y-j->y)^2; } void f2(void){ drawline((struct point){.x=1, .y=1},(struct point){.x=3, .y=4}); drawline2(&(struct point){.x=1, .y=1},&(struct point){.x=3, .y=4}); printf("%f ",(const float []){1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6}); printf("%s ","/tmp/fileXXXXXX"); printf("%s ",(char []){"/tmp/fileXXXXXX"}); printf("%s ",(const char []){"/tmp/fileXXXXXX"}); printf("%d ",(const char []){"abc"} == "abc"); eval(endless_zeros); } struct s { int i; }; int f3(void) { struct s *p = 0, *q; int j = 0; again: q = p, p = &((struct s){ j++ }); if (j < 2) goto again; return p == q && q->i == 1; } int main(void){ f(); f2(); f3(); return printf("\n6.5.2.5 Compound literals %d\n",*p ); } // warning will be //c2011-6-5-2-5-ex.c:20:23: warning: format specifies type 'int' but the argument has type 'struct int_list' [-Wformat] // return printf(" %d ",l); // ~~ ^ //c2011-6-5-2-5-ex.c:48:14: warning: format specifies type 'double' but the argument has type 'const float *' [-Wformat] //printf("%f ",(const float []){1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6}); // ~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //c2011-6-5-2-5-ex.c:53:39: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare] // printf("%d ",(const char []){"abc"} == "abc"); // ^ ~~~~~ //3 warnings generated. // output will be // 2 0.000000 /tmp/fileXXXXXX /tmp/fileXXXXXX /tmp/fileXXXXXX 0 0 // 6.5.2.5 Compound literals 2