Thread: Variadic function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144
    Now, as to the variadic function question,

    It doesn't seem possible to do something like:

    Code:
    int safeprint ( char* string , ... ) 
    {
       printf ( string , ... )
    }
    The only way to deal with the different possibilities is to manually step through the input string and printf each item as it comes up. This means that my safeprint function will have to be able to cope with every conceivable format specifier that printf can handle.

    I think it would be easier to have different functions, like :
    Code:
    int safeprint ( char* string ) ;
    int safeprint2s ( char* string1 , char* string2 ) ;
    int safeprintui ( unsigned int i ) ;
    Is that the best idea? (Other than getting treatment for OCD)

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    int safeprint ( char* string , ... ) 
    {
       va_list va;
    
       va_start(va, string);
       count = vprintf(string, va);
       va_end(va);
       // insert silly error checking here
    }
    Also see vsprintf, vfprintf, vsnprintf, etc.

    As far as OCD, I'd never joke about it. My brother suffers terribly from it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

Tags for this Thread