pass on variable arguments

This is a discussion on pass on variable arguments within the C Programming forums, part of the General Programming Boards category; At first I felt a strong urge to introduce my self and my programming experience. But came to the conclusion ...

  1. #1
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    67

    pass on variable arguments

    At first I felt a strong urge to introduce my self and my programming experience. But came to the conclusion that I would only look silly. When it comes down to it, I don't know ........ about c, but I want to learn. That's what brings me here...

    I wonder how to pass on a variable amount of variables to another function. Is it possible? In short... I want to make a function that passes the arguments on.

    Code:
    void write(char *msg, ...) {
      va_list args;
      bla bla bla
      printf(msg, args?)
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Search the forum for va_list and you should get loads of info on it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You can't with normal functions. But some functions take va_list parameters, and that lets you do it. For example, you can use vfprintf() like so.

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    #include <string.h>
    #include <time.h>
    
    void log_message(const char *format, ...);
    
    int main() {
        log_message("Starting\n");
        
        printf("Press any key.\n");
        getchar();
        
        log_message("Ending, elapsed time = %d\n", (int)clock());
        
        return 0;
    }
    
    void log_message(const char *format, ...) {
        time_t t;
        const char *time_string;
        va_list args;
        va_start(args, format);
        
        time(&t);
        time_string = ctime(&t);
        printf("[%.*s] ", strlen(time_string) - 1, time_string);
        vfprintf(stdout, format, args);
        
        va_end(args);
    }
    Input/output:
    Code:
    [Thu Sep 10 16:40:48 2009] Starting
    Press any key.
    
    [Thu Sep 10 16:40:50 2009] Ending, elapsed time = 0
    [edit] Quzah types too fast. Or, more likely, I type too much. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks View Post
    Quzah types too fast. Or, more likely, I type too much.
    It's the colors. The colors get me every time.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    67
    Thanks for that information.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    Taking dwks' example, you would also write a

    vlog_message(const char *format, va_list args );

    to do all the actual work, and the variadic function is just a simple wrapper to convert the ... into a va_list to be passed from one vfunc() to another without any further complication.

    If someone else decides to further wrap your function, the existence of the vfunc() will make it SO much easier for them to do.
    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. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. variable arguments in functions
    By Stonehambey in forum C++ Programming
    Replies: 11
    Last Post: 02-22-2008, 04:10 AM
  3. A function accepting a variable number of arguments.
    By Lithorien in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2004, 01:48 AM
  4. How to pass variable to Windows System?
    By jongmin in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2004, 04:59 PM
  5. passing arguments
    By Baard in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2003, 07:10 AM

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