parameter passing to sprintf

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

parameter passing to sprintf

2,973件の閲覧回数
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
 
ラベル(1)
0 件の賞賛
返信
1 返信

1,078件の閲覧回数
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 件の賞賛
返信