Thread: Variables number of arguments error

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    722

    Variables number of arguments error

    I'm having troubles parsing a variable list of arguments...
    Code:
    #include <stdarg.h>
    #include <stdio.h>
    
    void debug_output(const char *s, ...){
    	va_list arglist;
    //#ifdef _DEBUG
    	if(!s) return;
    	va_start(arglist, s);
    	fprintf(stderr,s,arglist);
    	va_end(arglist);
    //#endif
    }
    
    int main(){
    	debug_output("Exp 0\n");
    	debug_output("Exp 1: %s\n","asd");
    	debug_output("Exp 2: %s %s\n","abc","def");
    	debug_output("Exp 3: %d\n",1);
    	debug_output("Exp 4: %d %d\n",6,7);
    	return 0;
    }
    And the output:
    Code:
    Exp 0
    Exp 1: XPM
    Exp 2: ,PM └↕
    Exp 3: 1244976
    Exp 4: 1244972 1245056
    Clearly the list isn't being well formed... Any sugestions?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Try vfprintf().

    gg

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're not using va_arg(). Try replacing:
    Code:
    fprintf(stderr,s,arglist);
    with...
    Code:
    fprintf(stderr, s, va_arg(arglist, char *));
    Of course that only works with strings. vfprintf() is another way to go
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Yeap, vprintf worked. Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM