Kamis, 03 Desember 2015

stdarg

C Library <stdarg.h>

<stdarg.h>
This header defines macros to access the individual arguments of a list of unnamed arguments whose number and types are not known to the called function.

A function may accept a varying number of additional arguments without corresponding parameter declarations by including a comma and three dots (,...) after its regular named parameters:
return_type function_name (parameter_declarations,...);
 
To access these additional arguments the macros va_start, va_arg and va_end, declared in this header, can be used:
  • First, va_start initializes the list of variable arguments as a va_list.
  • Subsequent executions of va_arg yield the values of the additional arguments in the same order as passed to the function.
  • Finally, va_end shall be executed before the function returns.


Library Variable
  • va_list
  1. This is a type suitable for holding information needed by the three macros va_start(), va_arg() and va_end().
  2. Before a function that has initialized a va_list object with va_start returns, the va_end macro shall be invoked.
  3. The specifics of this type depend on the particular library implementation. Objects of this type shall only be used as argument for the va_start, va_arg and va_end, and va_copy macros, or functions that use them, like the variable argument functions in <cstdio.h>


Library Macros
  • va_start
  1. The C library macro void va_start(va_list ap, last_arg) initializes apvariable to be used with the va_arg and va_end macros.
  2. The last_arg is the last known fixed argument being passed to the function i.e. the argument before the ellipsis/(,...).
  3. This macro must be called before using va_arg and va_end.
  4. Declaration: void va_start(va_list ap, last_arg);
  5. parameter -> ap - This is the object of va_list and it will hold the information needed to retrieve the additional arguments with va_arg.
  6. last_arg − This is the last known fixed argument being passed to the function.

  • va_arg
  1. The C library macro type va_arg(va_list ap, type) retrieves the next argument in the parameter list of the function with type.
  2. This does not determine whether the retrieved argument is the last argument passed to the function.
  3. Declaration: type va_arg(va_list ap, type)
  4. parameter -> ap - This is the object of type va_list with information about the additional arguments and their retrieval state. This object should be initialized by an initial call to va_start before the first call to va_arg.
  5. parameter -> type - This is a type name. This type name is used as the type of the expression, this macro expands to.
  6. This macro returns the next additional argument as an expression of type type.

  • va_end
  1. The C library macro void va_end(va_list ap) allows a function with variable arguments which used the va_start macro to return.
  2. If va_end is not called before returning from the function, the result is undefined.
  3. Declaration: void va_end (va_list ap)
  4. parameter -> ap - This is the va_list object previously initialized by va_start in the same function.
  5. This macro does not return any value.


Example 1:
#include <stdarg.h>
#include <stdio.h>

int sum(int, ...);
int main()
{
   printf("Sum of 15 and 56 = %d\n",  sum(2, 15, 56) );
   return 0;
}
int sum(int num_args, ...)
{
   int val = 0;
   va_list ap;
   int i;

   va_start(ap, num_args);
   for(i = 0; i < num_args; i++) 
   {
      val += va_arg(ap, int);
   }
   va_end(ap);
   return val;
}


Example 2:
#include <stdarg.h>
#include <stdio.h>

int mul(int, ...);
int main()
{
   printf("15 * 12 = %d\n",  mul(2, 15, 12) );
   return 0;
}
int mul(int num_args, ...)
{
   int val = 1;
   va_list ap;
   int i;

   va_start(ap, num_args);
   for(i = 0; i < num_args; i++) 
   {
      val *= va_arg(ap, int);
   }
   va_end(ap);
   return val;
}

Tidak ada komentar:

Posting Komentar