args not an array of arguments. This is only the first argument that you passed first . Thus, its value in this case is the q value.
You cannot iterate through va args as you do.
Do it:
va_start(ap, args); do { r = va_arg(ap, char*); if (r != NULL) break; } while (1); va_end(ap);
This will crash if you don't have an argument other than NULL, so you better pass the number of arguments as the first argument:
char *first(int nargs, ...) { char *r = NULL; va_start(ap, nargs); for( ; nargs; nargs--) { r = va_arg(ap, char*); if (r != NULL) break; } va_end(ap); return r; } first(4, q, w, e, r);
Alternatively, use a sentinel signal:
char *first(char *first, ...) { char *r = first; va_start(ap, first); while (!r) { r = va_arg(ap, char*); } va_end(ap); return r == &sentinel ? NULL : r; } char sentinel;
source share