S
Shao Miller
Does anyone reading this message ever use this pattern for automatically
counting arguments and passing that argument count to a function?
#include <stdarg.h>
#include <stdio.h>
/*** Macros */
#define CountOf(array) (sizeof (array) / sizeof *(array))
/*** Function declarations */
void test(int arg_count, ...);
#define test(...) test(CountOf(((int[]){__VA_ARGS__})), __VA_ARGS__)
/*** Function definitions */
int main(void) {
int i, j, k;
i = j = k = 42;
test(i, ++j, k++);
return 0;
}
#undef test
void test(int arg_count, ...) {
va_list vargs;
int arg;
if (!arg_count) {
printf("No arguments passed!\n");
}
printf("{ ");
va_start(vargs, arg_count);
while (arg_count--) {
arg = va_arg(vargs, int);
printf("%d, ", arg);
continue;
}
va_end(vargs);
printf("}\n");
return;
}
counting arguments and passing that argument count to a function?
#include <stdarg.h>
#include <stdio.h>
/*** Macros */
#define CountOf(array) (sizeof (array) / sizeof *(array))
/*** Function declarations */
void test(int arg_count, ...);
#define test(...) test(CountOf(((int[]){__VA_ARGS__})), __VA_ARGS__)
/*** Function definitions */
int main(void) {
int i, j, k;
i = j = k = 42;
test(i, ++j, k++);
return 0;
}
#undef test
void test(int arg_count, ...) {
va_list vargs;
int arg;
if (!arg_count) {
printf("No arguments passed!\n");
}
printf("{ ");
va_start(vargs, arg_count);
while (arg_count--) {
arg = va_arg(vargs, int);
printf("%d, ", arg);
continue;
}
va_end(vargs);
printf("}\n");
return;
}