sprintf and strings

I really can't get my head round this. I want to do a fastrprint of a string - so I sprintf it - but it hangs on this part. I have made a short bit of code to recreate this. Any help would be greatly appreciated.

void setup() {
  Serial.begin(115200);
  writeLogEvent(0,"Startup Complete 002"); 

}

void writeLogEvent(int Err, String Info) {
  char stringBuffer8[8];          // Must be the correct size tr it hangs
  char stringBuffer80[80];        // fastprint is limited to 90 characters so this must be < 90

  Serial.print ("Err (");
  Serial.print (Err);
  Serial.print ("), Info (");
  Serial.print (Info);
  Serial.println (")");

  Serial.println ("Write Error");
  sprintf (stringBuffer8,"%d",Err);

  Serial.println ("Write Info");

  //  sprintf (stringBuffer80,"%s \0","Startup Complete 001");    // This Works

  sprintf (stringBuffer80,"%s \0",Info);    //  this hangs


  Serial.println ("END");
};


void loop() {
}

void writeLogEvent(int Err, String Info)Is the text that you are passing to this function actuall a String and even if it is, doesn't the %s parameter of sprintf() expect a C style string rather than a String ?

Try this

void setup() {
  Serial.begin(115200);
  char * astring = {"Startup Complete 002"};
  writeLogEvent(0, astring);
}

void writeLogEvent(int Err, char * Info) {
  char stringBuffer8[8];          // Must be the correct size tr it hangs
  char stringBuffer80[80];        // fastprint is limited to 90 characters so this must be < 90

  Serial.print ("Err (");
  Serial.print (Err);
  Serial.print ("), Info (");
  Serial.print (Info);
  Serial.println (")");

  Serial.println ("Write Error");
  sprintf (stringBuffer8, "%d", Err);

  Serial.println ("Write Info");

  //  sprintf (stringBuffer80,"%s \0","Startup Complete 001");    // This Works
  sprintf (stringBuffer80, "%s \0", Info);
  Serial.println ("END");
}

void loop() {
}

Yes this works! Perfect and thanks so much.

I didn't realise that there was a difference between String and a C type string.

Thanks again

sprintf (stringBuffer80,"%s \0",Info);    //  this hangs

Not sure what you're trying to accomplish here, but that \0 serves no useful purpose....

Regards,
Ray L.

I think I was attempting to terminate the string... but I guess from your comment that's not needed.

I guess from your comment that's not needed.

Yes and no. The terminating '\0' is needed but is already there in the string.

When you write something like:

  char msg[] = "Hello World";

the compiler places that string somewhere in memory. Let's say memory address 100. In memory, that string looks like:

101 103 105 107 109 111
+---+---+---+---+---+---+---+---+---+---+---+---+
| H | e | l | l | o | | W | o | r | l | d | \0 |
+---+---+---+---+---+---+---+---+---+---+---+---+
100 102 104 106 108 110

The last character in the string is the null termination character, '\0', and marks the end of the string. That's why UKHeliBob's answer was the way it is. The null termination character is needed so the compiler can sense the end-of-string, but with string constants like the example here, the compiler supplies the null. Because the null byte is necessary, always define a string constant with an extra byte or let the compiler figure it out when you define the string.