// filename:c2011-7-16-1-4-ex.c // original examples and/or notes: // (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011 // C2011 7.16.1.4 The va_start macro // compile and output mechanism: // (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013 // compile errors and/or wornings: // (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. #include // Example 1 #include #define MAXARGS 31 void f2(int n, char ** array){ printf("f2 %d, %s ", n,array[n]); } void f4(int n, char ** array){ printf("f4 %d, %s ", n,array[n]); } void f1(int n_ptrs, ...) { va_list ap; char *array[MAXARGS]; int ptr_no = 0; if (n_ptrs > MAXARGS) n_ptrs = MAXARGS; va_start(ap, n_ptrs); while (ptr_no < n_ptrs) array[ptr_no++] = va_arg(ap, char *); va_end(ap); f2(n_ptrs, array); } // Example 2 //#include //#define MAXARGS 31 void f3(int n_ptrs, int f4_after, ...) { va_list ap, ap_save; char *array[MAXARGS]; int ptr_no = 0; if (n_ptrs > MAXARGS) n_ptrs = MAXARGS; va_start(ap, f4_after); while (ptr_no < n_ptrs) { array[ptr_no++] = va_arg(ap, char *); if (ptr_no == f4_after) va_copy(ap_save, ap); } va_end(ap); f2(n_ptrs, array); // Now process the saved copy. n_ptrs -= f4_after; ptr_no = 0; while (ptr_no < n_ptrs) array[ptr_no++] = va_arg(ap_save, char *); va_end(ap_save); f4(n_ptrs, array); } int main(void) { char * msg[] = {"C2011","ISO/IEC9899","N1570","April12,2011","7.8.1", "Macros","for","format","specifiers"}; int i=4, j=2; f1(j,msg);f3(j,i,msg); return printf("7.16.1.4 The va_start macro\n"); } // output1(LLVM3.2) may be //f2 2, (null) f2 2, ?f?f? f4 -2, UH??I??H??H)?H9?r5H??Pw4????t?H???H????u???t?H?ƈH????u?L??]?? 7.16.1.4 The va_start macro // output2(GCC4.9) // f2 2, (null) f2 2, h?? f4 -2, ? // ^? 7.16.1.4 The va_start macro