return of char* function

char* myFunction1(boolean test) //Function 1
{
  static char variable[10];

  if(test)
  {
    strcpy(variable, "something");
    return variable;
  }

  return "";
}


char* myFunction2(boolean test) //Function 2
{
  static char variable[10];

  if(test)
  {
    strcpy(variable, "something");
  }
  else
  {
    strcpy(variable, "");
  }

  return variable;
}

I was thinking about this. Let's say I wish to return an empty cstring if "test" is false. From the above 2 functions, is function 2 the correct way of returning a char*? I am guessing function 1 will return an incorrect pointer because no static variable is assigned to it, and thus it is pointing to an incorrect memory location?

I have tried to test it with something like this:

if (myFunction1(false)[0] == '\0' )
  {
    Serial.println("ok");
  }

if (myFunction2(false)[0] == '\0' )
  {
    Serial.println("ok");
  }

But both printed "ok". So I am obviously not sure if function 1 is also correct now. Could someone advise.

The first one is incorrect as you return a pointer that points to nowhere if test is false; it will work reliably if you return NULL

char* myFunction1(boolean test) //Function 1
{
  static char variable[10];

  if(test)
  {
    strcpy(variable, "something");
    return variable;
  }

  return NULL;
}

void loop()
{
  char *result = myFunction1(false);
  if(result == NULL)
  {
    // no result returned
  }
  else
  {
    // result returned
  }

or you could do something like this:

void setup() 
{
  Serial.begin(9600);
  bool someVar = true;
  if(char* message = myFunction(someVar))
  {
    Serial.print(message);
  } 
  Serial.println("done");
}

void loop() {}

char* myFunction(bool test)
{
  static char myString[30];
  if(test)
    return strcpy(myString, "something");
  else 
    return nullptr;
}

similar...

Thanks for the replies. "return a pointer that points to nowhere" is one of the things what I needed to know. I have thought of returning NULL, but the thing is I sometimes would like to use the returned value directly in a strcmp function.

So I noticed that if the result returned is NULL, and I don't do additional checking for it first, it will crash. But if I return an empty string instead, the strcmp function won't crash.

Edit: I just tested, returning NULL/nullptr in strcmp function on Arduino does not crash, but crashes on ESP8266. Well well now I know.

Byork:
But I noticed BulldogLowell used nullptr instead of NULL, I'm not sure of the difference at this moment or if it will make a difference. I will test it soon when I get a chance.

I think that using the nullptr keyword improves the readability of the code.

this is C++ 11, there are reasons to use nullptr vs NULL as advised by Stroustrup himself.

:slight_smile:

Thanks for the link, it has a good concise answer.

I would expect both to functions work. Are not string constants assigned static addresses?

One change you should do, at least for myFunction1(): Make the return type 'const chr *'. Without the 'const' keyword and with warnings turned up you get a warning when you try to return "" as a 'char *'.