Chapter Contents

Previous

Next
va_arg

va_arg



Access an Argument from a Varying-Length Argument List

Portability: ISO/ANSI C conforming


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdarg.h>

(arg_type) va_arg(va_list ap, arg_type);


DESCRIPTION

va_arg returns the value of the next argument in a varying-length argument list.

The first argument, ap , is a work area of type va_list , which is used by the expansions of the various <stdarg.h> macros. (The va_list must be initialized by a previous use of the va_start macro, and a corresponding va_end must not have been used.)

The second argument, arg_type , is the type of the argument that is expected. arg_type must be written in such a form that arg_type * is the type of a pointer to an element of that type. For example, char is a valid arg_type because char * is the type of a pointer to a character. int(*)() is not a valid second argument to va_arg because int(*)()* is not a valid type. This is not a serious limitation because you can use typedef declarations to create usable synonyms of this sort for any type.

If the actual value passed is not of the type specified, the results are unpredictable.


RETURN VALUE

va_arg returns the value of the next argument in the list. The type is always the same as the second argument to va_arg .


CAUTION

The results of va_start are unpredictable if the argument values are not appropriate.

In certain cases, arguments are converted when they are passed to another type. For instance, char and short arguments are converted to int , float to double , and array to pointer. When parameters of this sort are expected, va_arg must be issued with the type after conversion. For example, va_arg(ap, float) may fail to access a float argument value correctly, so you should use va_arg(ap, double) .

There is no way to test whether a particular argument is the last one in the list. Attempting to access arguments after the last one in the list produces unpredictable results.


EXAMPLE

This example shows a function named concat , which can be used to concatenate any number of strings. A sample call is

concat(3, a, b, c);

This has the same effect as

strcat(a,b);
strcat(a,c);

(The first argument is the total number of strings.)

#include <stdarg.h>
#include <string.h>

void concat(int count, ...)
{
  va_list ap;
  char *target, *source;
  int i;

  if (count <= 1) return;

  va_start(ap, count);

  target = va_arg(ap, char *); /* Get target string.       */
  target += strlen(target);    /* Point to string end.     */

  while(--count > 0) {

        /* Get next source string.                         */
     source = va_arg(ap, char *);

                               /* Copy chars to target.    */
     while(*source) *target++ = *source++;
     }
  *target = '\0';              /* Add final null.          */
  va_end(ap);                  /* End arg list processing. */
  return;
}


RELATED FUNCTIONS

va_end , va_start


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.