How to write a function that returns an array?

Assuming you are correct about the shallow copy, you still need to have the array in the caller (for it to be copied into). So you don't achieve anything over passing that other array by reference anyway.

I qualified my answer with the word "really" because you can return arrays, sure:

  • If you don't mind the program crashing
  • If you return a static variable

But the static variable approach has flies on it. Say you return a pointer to this static array (which "lives" inside the function) then you can't afford to call the function twice and expect both variables to be valid. And that's easy enough to do:

char * bar (int i)
  {
  static char buf [10];
  itoa (i, buf, 10);
  return buf;
  }

void setup ()
{
  Serial.begin (115200);
  char foo [50];
  sprintf (foo, "%s %s", bar (1), bar (2));
  Serial.println (foo);
}

void loop () {}

This outputs:

2 2

Which might not be what you expect.