Args after varargs

This is a discussion on Args after varargs within the C Programming forums, part of the General Programming Boards category; Hi all, Have a lil problem here, I have a function prototype that looks a little like this log(char *app, ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Args after varargs

    Hi all,

    Have a lil problem here,

    I have a function prototype that looks a little like this

    log(char *app, char *msg, char *user, int flag)

    I was then told to include var args for msg, so that people can log data with the olde %d, %s etc.

    Now id i use var args, how do i ensure that the last 2 variables get passed?
    I cannot change the ordering as a lot of programs depend on it

    Advise,
    Arun

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Well you could still use varargs, except that perhaps the interface would be a little less intuitive since the arguments would be appended with two arguments in between them and the format string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    varargs doesn't depend on the format string being the last parameter.
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    void __attribute__ ((format (printf, 2, 5)))
    log(char *app, char *msg, char *user, int flag, ... )
    {
      va_list ap;
      va_start(ap,flag);
      printf("App=%s, user=%s, flag=%d\n",app,user,flag);
      vprintf(msg,ap);
      printf("\n");
      va_end(ap);
    }
    
    int main()
    {
      log("main", "Hey %s %d", "me", 42, "A message", 1234 );
      return 0;
    }
    If you're using gcc, then you'll want to keep the __attribute__ thing so that you get free checking of the variable parameters (just like if you were using regular printf).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Hi,

    Thanks for the replies. But the problem now is that my logging apparatus becomes very non intuitve to use, as the formatting parameters are at the very end.

    Output for your code snippet:

    App=main, user=me, flag=42
    Hey A message 1234

    Rather, is there a way to find out the number of arguments that have been passed to a function, so I may capture the last 2 and proceed to process the rest?

    Arun

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    So use vsnprintf() to format msg and the variable arguments into a buffer, then use that buffer along with the rest of the parameters in the way you're doing it at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Hey, do we get a mention in your commercial product?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-24-2005, 01:32 PM
  2. Replies: 1
    Last Post: 06-29-2004, 05:23 PM
  3. popen with args?
    By mart_man00 in forum Linux Programming
    Replies: 4
    Last Post: 07-08-2003, 09:47 PM
  4. Multiple Command Line Args
    By mart_man00 in forum C Programming
    Replies: 10
    Last Post: 08-16-2002, 02:15 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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