Using va_args

I must be suffering brain fade as I cannot seem to make the passing of variable number of arguments to a variable number of argument function work.

Here is the troublesome code. My function I need to get working is SMP

Someone please point out what I've messed up with please

Alan

// function prototype, ultimately to go into a .h file
void SMP(char *sFmt, ...);

void setup() {
  Serial.begin(19200);
  // initialise the printf family
  printf_begin();
}

void loop() {
  SMP("Hi %s %s\n", "there ", "world");
  delay(200);
}

// this is the troublesome function
// ... to pass variable number of arguments
void SMP(char *sFmt, ...)
{
  char acTmp[128];       // place holder for sprintf output
  va_list args;          // args variable to hold the list of parameters
  va_start(args, sFmt);  // mandatory call to initilase args 

  // the following call should (hopefully) pass the variable
  // arguments to the sprintf function which in turn handles variable arguments
  sprintf(acTmp, sFmt, args);
  // but it prints rubbish after the initial "Hi "

  // the line below will make the first parameter, "there" print
  // but not the second one, "world"
  // I didn't expect this to be the right way to do it
  // Just included as part of my testing
  // sprintf(acTmp, sFmt, va_arg(args, char *));

  // send it out to the serial port/monitor
  printf(acTmp);

  // mandatory tidy up
  va_end(args);
  return;
}

// Helper functions for printf family calls --------------------
int serial_putc(char c, FILE *)
{
  Serial.write(c);
  return c;
}
void printf_begin(void)
{
  fdevopen(&serial_putc, 0);
  return;
}

I think that what you are trying to use is this:
http://www.cplusplus.com/reference/cstdio/vsprintf/

luisilva:
I think that what you are trying to use is this:
http://www.cplusplus.com/reference/cstdio/vsprintf/

It is and I think I've done it correctly... but obviously not.

I need an experienced eye to look over my code and see where I've messed up

But you are doing it wrong. You mus use vsprintf() and you are using sprintf() instead, don't?

luisilva:
But you are doing it wrong. You mus use vsprintf() and you are using sprintf() instead, don't?

Wahey, that's it... the missing 'v'. I'd been doing loads of stuff with sprintf I got blinded

Thank you luisilva.

Now fitted into my code and working very nicely... time for bed I think and fresh brain cells in the morning