Vararg Functions


Typedefs

typedef char* va_list

Functions

void va_start (va_list ptr_arg, prev_param)
type va_arg (va_list ptr_arg, type)
void va_end (va_list ptr_arg)


Detailed Description

Some functions for handling variable numbers of arguments (like those in stdarg.h).


Typedef Documentation

typedef char * va_list
 

Storage to hold information needed by the va_start(), va_arg() and va_end() macros.
The called function declares a variable of type va_list that can be passed as an argument to another function.

See also:
va_start, va_end, va_arg.


Function Documentation

type va_arg ( va_list ptr_arg,
type )
 

Retrieves the current argument from the list of optional arguments for variable argument function.

Parameters:
ptr_arg   A pointer to the list of arguments
type   Type of the argument to retrieve
Returns:
Current argument from the list of optional arguments
      #include <cybiko.h>
      ...
      //  Traces strings.
      void trace_multistring(int string_number, ...)
      {
        int index;
        va_list parameters;
        va_start( parameters, string_number );
        for( index = 0; index < string_number; index ++ )
        {
          TRACE( va_arg( parameters, char* ) );
        }
        va_end( parameters );
      }
      ...
      trace_multistring( 4, "One", "Two", "Three", "Four");
      ...
See also:
va_start, va_end.

void va_end ( va_list ptr_arg )
 

Releases a list of optional arguments for variable argument function.

Parameters:
ptr_arg   A pointer to a list of arguments
Returns:
None
      #include <cybiko.h>
      ...
      //  Logs errors to the file.
      void trace2logfile(char* sz_format, ...)
      {
        va_list parameters;
        char sz_buffer[64];
        struct FileOutput* ptr_log_file; 
        //  Formats string.
        va_start( parameters, sz_format );
        vsprintf( sz_buffer, sz_format, parameters );  
        va_end( parameters );
        //  Writes it to file "error.log".
        ptr_log_file = (struct FileOutput*)malloc( sizeof( struct FileOutput ) );
        FileOutput_ctor_Ex( ptr_log_file, "error.log", TRUE );
        FileOutput_seekp( ptr_log_file, 0, SEEK_END );
        FileOutput_write( ptr_log_file, sz_buffer, strlen(sz_buffer) );
        FileOutput_dtor( ptr_log_file, FREE_MEMORY );
      }
      ...
See also:
va_start, va_arg.

void va_start ( va_list ptr_arg,
prev_param )
 

Initializes a list of optional arguments for variable argument function.

Parameters:
ptr_arg   A pointer to list of arguments
prev_param   The parameter that precedes the first optional argument
Returns:
None
      #include <cybiko.h>
      ...
      //  Logs errors to the file.
      void trace2logfile(char* sz_format, ...)
      {
        va_list parameters;
        char sz_buffer[64];
        struct FileOutput* ptr_log_file; 
        //  Formats string.
        va_start( parameters, sz_format );
        vsprintf( sz_buffer, sz_format, parameters );  
        va_end( parameters );
        //  Writes it to file "error.log".
        ptr_log_file = (struct FileOutput*)malloc( sizeof( struct FileOutput ) );
        FileOutput_ctor_Ex( ptr_log_file, "error.log", TRUE );
        FileOutput_seekp( ptr_log_file, 0, SEEK_END );
        FileOutput_write( ptr_log_file, sz_buffer, strlen(sz_buffer) );
        FileOutput_dtor( ptr_log_file, FREE_MEMORY );
      }
      ...
See also:
va_end, va_arg.