Thread: variable number of arguments for a function or a macro

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    variable number of arguments for a function or a macro

    I need to give a unique interface for different implementations of outputs for different platforms. To do this i need to define a prototype with a variable number of arguments in the end like printf() but i don't know : how to do this?

    It should be something like this:
    Code:
    int SPRINTF(char * string, const char * FormatString, void * arg1, void * arg2,..)
    {
         #if LINUX
              snprintf(string, sizeof(string), FormatString, *arg1, *arg2,..);
         #else if WINDOWSCE
              sprintf(string, FormatString, *arg1, *arg2,..);
         #endif
    
    }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    man stdarg

    That's all there is to it. It is actually quite simple to understand how to do it. You'll define your function as
    Code:
    int printf(const char *fmt, ...)
    then you'll define a va_list and go from there.

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Kennedy View Post
    man stdarg

    That's all there is to it. It is actually quite simple to understand how to do it. You'll define your function as
    Code:
    int printf(const char *fmt, ...)
    then you'll define a va_list and go from there.
    I am afraid I don't understand. If i write the dots I suppose i get a compiler error.. My code specified was actually pseudocode. How to reference the vars then? What is a va_list?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by mynickmynick View Post
    I am afraid I don't understand. If i write the dots I suppose i get a compiler error.. My code specified was actually pseudocode. How to reference the vars then? What is a va_list?
    This site might help.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by mynickmynick View Post
    I am afraid I don't understand. If i write the dots I suppose i get a compiler error.. My code specified was actually pseudocode. How to reference the vars then? What is a va_list?
    Google (above) has the answers or just type MAN STDARG from the command line (if you are on a *nix box) or put MAN STDARG into your favorite search engine.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Handy linux manpage viewer written by me a few years ago when I was a cboard newbie!

    See: text and manpage viewer for linux

    I may totally re-write it in C++ someday soon and add some features (like tabbing) but it works fine, does apropos searches, keeps a history, bookmarks, lets you add notes to the man pages without altering them, etc, and is an amazingly lite exe. I use it all the time. Also there is an official deb package now.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM