c - How to loop through a va_list if the number of arguments is unknown? -
how loop through va_list if number of additional arguments unknown?
#include <stdio.h> #include <stdarg.h> int add(int x, int y, ...) { va_list intargs; int temp = 0; va_start(intargs, y); int i; (i = 0; < 3; i++) { /* how can loop through number of args? */ temp += va_arg(intargs, int); } va_end(intargs); return temp + x + y; } int main() { printf("the total %d.\n", add(1, 2, 3, 4, 5)); return 0; }
use sentinel value terminator, e.g null
or -1
Comments
Post a Comment