parameter passing to sprintf

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

parameter passing to sprintf

2,391 Views
nanoGeek
Contributor I
I'm writing for the MCF5407 on CW6.2.
 
I need to write a wrapper around sprintf that has the same syntax as sprintf but with an additional parameter.  My funtcion will strip out the new parameter for processing, but needs to pass the remaining params to sprintf.  My new function needs to have the same syntax as sprintf with the addition of the new param.
 
My problem has been with handling the value list which follows the format string.  Does anyone have any tricks to pass along for doing this?
 
Here is a skeleton of what I am trying to do:
 
int dprintf(int flag, char* fmt, ... )
{
   extern char global_string[];
 
   // process flag
   return sprintf(global_string, fmt, ... );
}
 
I tried doing some pointer math against fmt, giving me a pointer to the next argument in the dprintf list:
 
asm int
dprintf(int flag, const char *fmt, ... )
{
  char * ap;
   extern char OUTSTR[];
 
  if((flag & 0x01) == 0)
  {
     return 0;
  }
  ap = (char*) ((char *)&fmt + sizeof(fmt));
  return sprintf(OUTSTR, fmt, ap);
}
However, sprintf simply processed the pointer as the first value for the fmt string.
 
Thanks,
 
Joe
 
Labels (1)
0 Kudos
1 Reply

496 Views
zkwayne
Contributor I

You should be using the helpers in stdarg.h to access these arguments. Instead of calling sprintf you will need to call vsprintf. sprintf actually ends up calling vsprintf. This should all be covered in any good book on C.

Note that pointer arithmetic is a no-no in this case as some of the arguments may be passed in registers. The way the arguments are passed can be compiler specific and might change based on compile time options, especially options that effect the optimizations.

 

0 Kudos