non-novice needs help with variable arguements va_arg to a function 
Author Message
 non-novice needs help with variable arguements va_arg to a function

Hi.  

I am trying to write a portable function that takes a variable number
of arguements and it calls another function with a variable number of
arguements, trying to pass the same optional arguements to that latter
function.  A shortened version of the code is shown below and it does
not work.  Help.

Thanks
Curtis
================================================================

#include <stdarg.h>

static void     log(              ts_t* pts );
static void     log_var(          ts_t* pts, const char* format, va_list argp );

int main( )
{
  log( pts );                                          
  activity_var( &( current.ts ), "%d",  ii );

Quote:
}

static void log( ts_t* pts )
{
  va_list temp;
  log_var( pts, NULL, temp );

Quote:
}

static void log_var( ts_t* pts, const char* format, va_list argp )
{
  static int           datai;
  register char        format_flag;                                    /* formatting character  */
  register size_t      len = 0;

  len = log_event_ts( pts, &( ang.log_buf ) );

  if ( format )
    {
                                                                /* Store the format string */
      va_start (argp, format);
      while ( format_flag = *format++ )
        {
          if ( format_flag != '%' )                                 /* process all standard characters, stop on NUL or '%' */
            continue;
         switch ( format_flag = *format++ )                        /* Special format flag is found */
            {
            case 'd':                                               /* Decimal output */
            case 'i':                                               /*  */
            case 'o':                                               /* Octal output */
            case 'u':                                               /* Unsigned integer */
            case 'x':                                               /* Hex output */
            case 'X':                                               /* Hex output */
                   datai = (int) va_arg(argp, int);               /* Get integer */
                   break;
            default:
                   break;
             }
      va_end( argp );
    }

Quote:
}

--



Sun, 11 Feb 2001 03:00:00 GMT  
 non-novice needs help with variable arguements va_arg to a function

[some lines/comments snipped so that netscape would send the message and
not say "message not sent: more included text than new"]

Quote:
> static void     log_var(ts_t* pts, const char* format, va_list argp );

Change this to

        static void log_var(ts_t* pts, const char* format, ...);

Quote:
> static void log( ts_t* pts )
> {
>   va_list temp;

This function does not take variadic arguments, so you don't need temp.
Drop it and change the function call to:

        log_var(ptr, NULL);

Quote:
> }

> static void log_var( ts_t* pts, const char* format, va_list argp )

Change this to

        static void log_var(ts_t* pts, const char* format, ...)

Quote:
> {
>   static int           datai;

You normally use static in local variables that shall retain their
contents between function invocations. If the value is not of use for
the succeeding function calls, you don't need it be static.

Quote:
>   register char        format_flag;
>   register size_t      len = 0;

You don't need to use 'register'. va_list works even if you have local
variables. 'register' is just a hint for the compiler that the values
could be stored in registers. Whether they will be, is up to the
implementation. What you do need is:

        va_list argp;

I would put

        va_start(argp, format);

here, but YMMV.

Quote:

>   len = log_event_ts( pts, &( ang.log_buf ) );

>   if ( format )
>     {
>       va_start (argp, format);

Remove va_start here. YMMV.

Quote:
>       while ( format_flag = *format++ )
>         {
>           if ( format_flag != '%' )
>             continue;
>          switch ( format_flag = *format++ )
>             {
>             case 'd':
>             case 'i':                                               /*  */
>             case 'o':
>             case 'u':
>             case 'x':
>             case 'X':
>                    datai = (int) va_arg(argp, int);
>                    break;
>             default:
>                    break;
>              }
>       va_end( argp );

Remove va_end here. YMMV.

Quote:
>     }

Here (YMMV), put the

        va_end(argp);

Quote:
> }

HTH,
        AriL

BTW, if at all possible, use lines of not more than 80 characters in
length. This will make them easier to read in browsers that wrap at 80
chars/line.
--
*DO NOT* send me email unless I ask you to.
I read the answers where I ask the questions.
This may also help some other people.
--



Mon, 12 Feb 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Functions using variable arguements

2. HELP:Pointers in function arguement

3. Help - I need to set variable A = value held in variable named in variable B

4. Need help with these function variable pointers?

5. static functions and non static variables

6. static functions with non-static variables!

7. novice needs help with recursion

8. Complete novice needs help!

9. A novice needs help...

10. Novice need your help!

11. Novice needs Help using a GNU lib on SGI

12. Novice need help

 

 
Powered by phpBB® Forum Software