another sprintf problem

i can get sprintf to work with an interger and %d in it but i cant get it to work with a string and %s in place of the %d. when the code hits that it just stops and never progresses past that point. why is this?

this works:

  char buffer[80];

int current = 65;
void setup()
{

  sprintf(buffer, "number = %d", current);
  delay(2000);
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println(buffer);

this doesn't:

  char buffer[80];
String current = "65";
void setup()
{

  sprintf(buffer, "number = %s", current);
  delay(2000);
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println(buffer);

it never prints buffer.

sprintf does not have an overload that takes a String object. So, it is the address that gets passed to the function, which it can't convert to a string.

Why are you trying to use sprintf for string manipulation anyway?

String out = "Some bs up front " + current;
Serial.println(out);

sprintf() wants a char *, not a String. Either try string.toCharArray() or change current to be a char *
One of these should work for you:

String current = "65";
sprintf(buffer, "number = %s", current.toCharArray());
char *current = "65";
sprintf(buffer, "number = %s", current);

thanks for the fast response guys. gardener the first way didnt work but the second worked like a charm. thanks for the help.

magwitch324:
the first way didnt work

Strange. On paper it should've worked -- I never use any of the C++ classes myself, so all I know about String is what's in the manual.

I'm strictly a raw C dude.

On closer inspection, the documentation included with Ardiuno 021 is inconsistent with the implementation. The documentation on the web site appears to match the implementation. http://arduino.cc/en/Reference/StringToCharArray

No surprise my suggestion wouldn't work.