John
John

Reputation: 1129

Should the count parameter be declared directly before the ellipsis (...) in a variadic function?

This is the implementation for the va_start macro:

#define va_start(list, param) (list = (((va_list)&param) + sizeof(param)))

As you can see, the va_start macro is returning the address of the first byte in the variable list of arguments by assuming that it exists directly after the count parameter (I mean by the count parameter the name of a parameter that I declare that I will pass to the number of arguments).

So if I am using in addition to the count parameter other parameters, Should the count parameter be declared directly before the ellipsis (...)?

Upvotes: 0

Views: 73

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

va_start() should always be called with the last named parameter, so for example

void function(int x, int y, int z, ...)
{
    va_list ap;

    va_start(ap, z);
    .
    .
    .
    va_end(ap);
}

So if I am using in addition to the count parameter other parameters, Should the count parameter be declared directly before the ellipsis (...)?

Yes, if you want to use va_start() this way

va_list ap;
va_start(ap, count);

Upvotes: 1

Related Questions