1

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.

Community
  • 1
  • 1
Kevin Dong
  • 5,001
  • 9
  • 29
  • 62

1 Answers1

6

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*.

this
  • 5,229
  • 1
  • 22
  • 51