va_arg question

M

mdh

May I clarify a few things about va_arg?

Given:

va_list ap;
int ival;

assume va_start initialized;


ival = va_arg(ap, int);


Is this correct.

The call to va_arg returns the **next** un-named argument. (after
va_start)
The **type** of argument is supplied by the second parameter. (here
"int").
The object returned is assigned to ( in this case) "ival".

Although I cannot think of a reason why you would not, but do you
always need to assign the return from va_arg?

Thanks
 
K

Keith Thompson

mdh said:
May I clarify a few things about va_arg?

Given:

va_list ap;
int ival;

assume va_start initialized;


ival = va_arg(ap, int);


Is this correct.

The call to va_arg returns the **next** un-named argument. (after
va_start)

Yes (except that I'm not entirely sure what "after va_start" means).
The **type** of argument is supplied by the second parameter. (here
"int").
Yes.

The object returned is assigned to ( in this case) "ival".
Yes.

Although I cannot think of a reason why you would not, but do you
always need to assign the return from va_arg?

No. For example, you might plausibly pass it as an argument to
some other function. Or you might compare it to something, or use
it as the expression in a switch statement, or even discard it.
va_arg(ap, int) expands an expression of type int; you can do
anything with it that you can do with any such expression.
 
B

Ben Bacarisse

mdh said:
Given:

va_list ap;
int ival;

assume va_start initialized;

ival = va_arg(ap, int);

Is this correct.
Yes.

The call to va_arg returns the **next** un-named argument. (after
va_start)
The **type** of argument is supplied by the second parameter. (here
"int").
The object returned is assigned to ( in this case) "ival".

Although I cannot think of a reason why you would not, but do you
always need to assign the return from va_arg?

No. If you want a semi-real example:

int all_true(int n, ...)
{
va_list ap;
va_start(ap, n);
while (n > 0 && va_arg(ap, int))
n -= 1;
va_end(ap);
return n == 0;
}
 
M

mdh

No.  If you want a semi-real example:

  int all_true(int n, ...)
  {
       va_list ap;
       va_start(ap, n);
       while (n > 0 && va_arg(ap, int))
            n -= 1;
       va_end(ap);
       return n == 0;
  }


thank you.
 
N

Nick Keighley

May I clarify a few things about va_arg?

Given:

va_list ap;
int ival;

assume va_start initialized;

ival = va_arg(ap, int);

Is this correct.

well to be pedantic, we don't have enough information.
The actual parameters may be wrong.

#include <stdarg.h>
#include <stdio.h>

void print_list (int n, ...)
{
va_list args;
int i;

va_start (args, n);

for (i = 0; i < n; i++)
printf ("%d ", va_arg (args, int));

printf ("\n");
va_end(args);
}

int main (void)
{
print_list (3, 1, 2, 3);
print_list (3, "apple", "mary", "dorien"); /* UB */
return 0;
}

<snip>

this is also an example of a vargs function that isn't
printf()-like and an example of a use of va_arg()
that doesn't assign the value
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top