[SOLVED] Why does my char* character string becomes garbaged?

Here's a tiny sketch to isolate the problem:

char* volts()
{
  float fl = (194 / 22.8) - 0.85;
  char str[10];
  int newFl = round(fl*100);
  sprintf(str,"%03d",newFl);
  str[4] = 0;
  str[3] = str[2];
  str[2] = str[1];
  str[1] = '.';
  Serial.println();
  Serial.print("internal: ");
  Serial.println(str);
  return str;
}

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  delay(4000);
  Serial.print(volts());
  Serial.println(" :external");
}

"7.66" is the correct result, and should be shown on both lines. But the second line (external to the function) becomes garbaged. Currently, I get:

internal: 7.66
7“½ :external

internal: 7.66
7“½ :external

internal: 7.66
7“½ :external

internal: 7.66
7“½ :external

Since this same syntax works perfectly for me elsewhere in my sketch, I don't know where to look for why this one instance is the exception.

str[] is a multiple byte local variable, defined internally to the function volts() and cannot be returned to the calling program.

Most people use a pointer in the call argument to return a character string.

You may or may not have another problem, but add str[0] = 'A';

Fox65535: You may or may not have another problem, but add str[0] = 'A';

You're going to have to talk us through that one.

Thanks, jremington. I put the "multiple byte variable above the volts() function to make it global instead of local, and that fixed everything! I guess my vabriable was being disposed of with the function before it had a chance to be read.

But your second line I don't understand. [UPDATE:] Didn't until AWOL explained it below. Thanks AWOL.

jremington: Most people use a pointer in the call argument to return a character string.

Want to give me an example of what that might be?

Fox65535: add str[0] = 'A';

I think you mean the byte str[0] is empty, so needs something in it so it doesn't mark the end of the string?

Actually, it does have a value in in it, because sprintf() startes filling a char string at character str[0], so it won't be empty. You saw I moved the other bytes around, but left str[0] alone, because the "7" in str[0] was already correct.

char* volts(char* str)
{

(Maybe with a buffer size, so your snprintf_P doesn't bust your buffer)