rerouting variable argument lists...

This is a discussion on rerouting variable argument lists... within the C++ Programming forums, part of the General Programming Boards category; i want to do some formatted output, but can i reroute an entire list of variable arguments that i send ...

  1. #1
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459

    rerouting variable argument lists...

    i want to do some formatted output, but can i reroute an entire list of variable arguments that i send to a function? something like...

    Code:
    void sprintf_w_extrastuff (char * msg, ...)
    {
      char * m = sprintf ("msg", INSERT_MAGIK_COMMAND_HERE);
    }
    such that it'll work nicely... i need this because instead of writing to the 80 by 20, i want to output it in a gfx/text type deal, but i don't want to have to put an sprintf inside my own displaying function, not even a macro for it... oh, and this is just pseudocode, so...

    thanks for your time! [bubba, know about this? ]
    hasafraggin shizigishin oppashigger...

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    You can do that but I don't remember how.
    // Gliptic

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    aw shucks, well i tried just inserting the ellipsis, but no dice... also, i could just go through the list by hand, but that would defeat the purpose of wanting to use sprintf to do it for me...

    shucks, c'mon remember!
    hasafraggin shizigishin oppashigger...

  4. #4
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86

    Smile

    Hey DA,

    I use somethin' like this as standard debug macro...Just have a look:

    Code:
    #include <stdarg.h>
    
    #define __DEBUGTRACE__ ENABLED
    
    #ifdef __DEBUGTRACE__
      static void DEBUG_TRACE (FILE *poStream, const char *pccMsg, ...)
      {
        if (pccMsg != 0)
        {
          va_list oArgs;
    
          if (!poStream)
          {
            poStream = stderr;
          };
    
          va_start (oArgs, pccMsg);
            fprintf (poStream, "DEBUG_MSG> ");
            vfprintf (poStream, pccMsg, oArgs);
          va_end (oArgs);
        };
      }
    #else
      //
      // if not in DEBUG mode, make DEBUG_TRACE an empty function,
      // that doesn't do anything.
      //
      static inline void DEBUG_TRACE (FILE*, const char*, ...) { }
    #endif
    enjoy!
    cody

    [ EDIT: Sorry, forgot to mention which library you need ]
    Last edited by cody; 12-14-2001 at 03:07 AM.
    #include "reallife.h"

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    I'm not sure what you are trying to do with that code. Sorry, but I'm going to need a bit more info in order to understand.

    I don't use variable amount of parameters with my functions.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    In that case, why don't simply pass the variables to the next function?
    // Gliptic

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    Found some info on it:

    From Borland help file
    [quote]
    Syntax

    #include <stdarg.h>
    void va_start(va_list ap, lastfix);
    type va_arg(va_list ap, type);
    void va_end(va_list ap);

    Description

    Implement a variable argument list.
    Some C functions, such as vfprintf and vprintf, take variable argument lists in addition to taking a number of fixed (known) parameters. The va_arg, va_end, and va_start macros provide a portable way to access these argument lists. They are used for stepping through a list of arguments when the called function does not know the number and types of the arguments being passed.
    The header file stdarg.h declares one type (va_list) and three macros (va_start, va_arg, and va_end).

    va_list: This array holds information needed by va_arg and va_end. When a called function takes a variable argument list, it declares a variable ap of type va_list.

    va_start: This routine (implemented as a macro) sets ap to point to the first of the variable arguments being passed to the function. va_start must be used before the first call to va_arg or va_end.

    va_start takes two parameters: ap and lastfix. (ap is explained under va_list in the preceding paragraph; lastfix is the name of the last fixed parameter being passed to the called function.)

    va_arg: This routine (also implemented as a macro) expands to an expression that has the same type and value as the next argument being passed (one of the variable arguments). The variable ap to va_arg should be the same ap that va_start initialized.

    Note: Because of default promotions, you cannot use char, unsigned char, or float types with va_arg.

    The first time va_arg is used, it returns the first argument in the list. Each successive time va_arg is used, it returns the next argument in the list. It does this by first dereferencing ap, and then incrementing ap to point to the following item. va_arg uses the type to both perform the dereference and to locate the following item. Each successive time va_arg is invoked, it modifies ap to point to the next argument in the list.

    va_end: This macro helps the called function perform a normal return. va_end might modify ap in such a way that it cannot be used unless va_start is recalled. va_end should be called after va_arg has read all the arguments; failure to do so might cause strange, undefined behavior in your program.

    Return Value

    va_start and va_end return no values; va_arg returns the current argument in the list (the one that ap is pointing to).[/list]

    As I said earlier, I don't use this and don't know anything more about it. It may be a good idea to use this sparingly in your program as it will introduce some confusion for you and maybe even some program instability if you do not use them correctly.
    Whatever you are trying to do, this is probably not the single best approach to the problem.

  8. #8
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    as i've said, if i just repass them in that manner, it would require requesting them with VA_XXXXX again, which defeats the purpose of having sprintf do it for me as well as translating them into my output string... oh, and bubba, correct usage of those functions is not as error prone as you might think...

    oh, and cody... y'know, passing the VA_ARGS structure instance just might work, i'll try that...
    hasafraggin shizigishin oppashigger...

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi

    instead of 'sprintf' just use 'vsprintf'

    damyan

  10. #10
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    oh yeah i did that, figured it out... i thought streams were ++ specific but lo and behold they were in my C: The Complete Reference... which comes through for me yet again...

    thanks all!!!
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-21-2009, 01:27 PM
  2. File Server Help
    By lautarox in forum C Programming
    Replies: 146
    Last Post: 09-24-2008, 06:32 PM
  3. Passing Through A Variable Argument List
    By SMurf in forum C Programming
    Replies: 6
    Last Post: 04-14-2007, 11:12 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Variable Argument Functions
    By genghis in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2002, 01:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21