Tomáš Zato Tomáš Zato - 10 days ago 6
C Question

Variable arguments without first argument - what belongs in va_start?

I want to make

varargs
function for freeing multiple pointers at once, mostly to clean up the code. So I have:

void free_all( ... ) {
va_list arguments;
/* Initializing arguments to store all values after last arg */
// but there are no args!
va_start ( arguments, ????? );
/* we expect the caller to send last argument as NULL **/
void* pointer = va_arg ( arguments, void* );
while( (pointer = va_arg ( arguments, void* ))!=NULL ) {
free(pointer);
}
va_end ( arguments ); // Cleans up the list
}


So what to put in
va_start ( arguments, ????? )
?

Answer

It's simply not possible. You MUST have a non vararg argument, always. In your case

void free_all(void *first, ...);

could work.