Home Segments Index Top Previous Next

623: Mainline

The required ellipses and calls to all four macros— va_list, va_start, va_arg, and va_end—are shown in the following version of construct_trade:

/* Define trade structure */ 
struct trade { 
    double price; 
    int number; 
}; 
/* Define constructor for trade structures with variable arguments */ 
struct trade* construct_trade (int argument_count, ...) { 
  va_list argument_pointer; 
  struct trade *tptr; 
  va_start (argument_pointer, argument_count); 
  tptr = (struct trade*) malloc (sizeof (struct trade)); 
  tptr -> price = va_arg (argument_pointer, double); 
  if (argument_count = 2) 
    tptr -> number = va_arg (argument_pointer, int);     
  else 
    tptr -> number = 100;     
  va_end (argument_pointer); 
  return tptr; 
}