Kevin Dong
Kevin Dong

Reputation: 5369

Usage of varargs in C

Here is the example of varargs.

I'm a little puzzled.

[x]Question 1: How to get the nth argument?

va_list ap;
va_arg (ap + (n-1), int); // **INCORRECT USAGE**

Is it correct? If not, how to get the nth argument?

[x]Question 2: When will ap increase?

It says ap will increase to the next argument, but if I use va_arg(ap + (n-1), int), n>=2, will ap increase?

Question 3: (BTW/maybe off-topic) What's ap abbreviated from?

Sometimes, we declared Cnt abbreviated from Count or Counter.

What's ap abbreviated from?

Question 4: Which type is ap?

I know it's va_list type, but what is va_list? struct? int? or something else?

Thanks.

Upvotes: 1

Views: 336

Answers (1)

this
this

Reputation: 5301

If you want the n-th argument you will have to call va_arg() with correct type n-1 times.

Your examples 1 and 2 are wrong. ap will be changed by va_arg() automatically.

Type of va_list is implementation defined, but is usually a char*.

Upvotes: 6

Related Questions