cancel
Showing results for 
Search instead for 
Did you mean: 

stdarg.h trashing parmN and then the variable arguments

scott239955
Associate II
Posted on October 06, 2003 at 06:36

stdarg.h trashing parmN and then the variable arguments

1 REPLY 1
scott239955
Associate II
Posted on May 17, 2011 at 11:35

I have been having problems using stdarg.h. I made a function:

LCD_printf(const char *c_ptr, ...)

{

va_list ap;

unsigned char c;

unsigned char c;

va_start(ap, c_ptr);

while (*c_ptr != '\0')

{

c = *c_ptr;

c_ptr++;

if (c == '%') {

d = *c_ptr;

c_ptr++;

d &= 0x0F;

c = va_arg(ap, unsigned char);

LCD_WrDec(c,d);

}

else

LCD_WrByte(c);

}

va_end(ap);

}

I call the function with something like this:

LCD_printf(''Data values: %1 & %2'', value1, value2);

The function worked the first couple of times, but then I added some things to it, it broke and I can not get it back to working. I added an extra argument and it worked for awhile, but then broke again. The problem is that it was trashing the *c_ptr variable value unless I remove the ellipses (, ...). The only way I could get it to work is adding a dummy variable so it can trash it instead of the character pointer:

LCD_printf(const char *c_ptr, unsigned char x, ...) { // x is dummy va_start(a_ptr, x);

I have to then call it with something like:

LCD_printf(''Data values: %1 and %2'', 0, value1, value2);

Well, that worked for awhile, but then it started giving me the wrong

values for the variable arguments. I'm wondering if this has ever been tested.

Any know issues or ideas?

[ This message was edited by: Smiles on 06-10-2003 10:10 ]