Thread: A function with infinite parameters

  1. #1
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107

    A function with infinite parameters

    I was wondering how exactly this is done. printf is usually one of the first functions you learn how to use, but no one really explains how it manages to allow you to plug in an endless amount of parameters. What I have in mind is a function that would allow you to merge a list of strings all into one, but I don't even know where to start...

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Check out stdarg.h http://www-ccs.ucsd.edu/c/stdarg.html

    Also note the c99 standard limits 127 arguments per function call.

    With that in mind consider this printf prototype:

    Code:
    int printf(const char *format, ...);
    Hope that helps.
    R.I.P C89

  3. #3
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Heh, no wonder they don't mention the deatails of that function to begginers. I know that would've scared me off before I even knew what a function was .

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You might also want to look here

    ~/

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by Xzyx987X
    I was wondering how exactly this is done. printf is usually one of the first functions you learn how to use, but no one really explains how it manages to allow you to plug in an endless amount of parameters. What I have in mind is a function that would allow you to merge a list of strings all into one, but I don't even know where to start...

    Also, keep in mind that variable-argument functions must have some means for detecting the end-of-data condition. Functions like printf() do that by simply counting the args in the format string 'by hand'. Another typical approach is to have the user pass some sentinel value (usually zero) to signal the end of data. Here's an example:


    Code:
     char * bigstrcat(char * buffer, const char * first, ...)
    {
     const char * next = first;
    
     va_list lst;
    
     va_start(lst, first);
     
         while(next != NULL)
        {
         strcat(buffer, next);        
         
         next = va_arg(lst, char*);
        } 
        
     va_end(lst);
        
     return buffer;
    }

    Which would be used like this:


    Code:
     int main()
    {
     char str[1024] = "Testing";
    
     bigstrcat(str, "...one", "...two", "...three.", NULL);
    
     printf("%s\n", str);
    
     return 0;
    }

    Another handy function is vsprintf(), which can be very useful for rolling custom made functions:


    Code:
     int log(FILE * out, int code, const char * message, ...)
    {
     const char 
     * success = "SUCCESS", 
     * warning = "WARNING", 
     * fatal   = "FATAL", 
     * type = code ?  code < 0 ? fatal : warning : success;  
    
     time_t now = time(NULL);
      
     char entry[(strlen(message) * 2) + 1024];
    
     va_list lst;
    
     va_start(lst, message);
    
     vsprintf(entry, message, lst);
    
     va_end(lst);
    
     fprintf(out, "%s Code [%d] (%s) '%s'.\n", 
       asctime(localtime(&now)), code, type, entry);
    
         if(code < 0) 
        {  
         fclose(out);
    
         exit(code);     
        } 
     
     return code;
    }
    
    
    
     int main()
    {
     log(stdout, 0, "Processed");
     
     log(stdout, 4, "Too many parameters");
    
     log(stdout, -6, "Internal exception");
    
     return 0;
    }
    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;
    }

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Also remember that the more parameters you pass the more cycles it will take to clean up the stack.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Huh - gcc cleans up the stack in constant time.
    It's usually
    add esp,num_bytes_taken_up_by_parameters
    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.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Umm......yeah.....oops.



    For some reason I thought he was pushing and popping all that crap onto the stack. But yes to clean it up is a simple subtraction. And yes I knew that because I've coded lots and lots of asm functions that can be used with C....but I must have had a brain fart or something.


    Doh!!!

    :bonk: -> cprog really needs this smilie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM