# Functions returning character arrays

**URL:** <https://forum.arduino.cc/t/functions-returning-character-arrays/1172359>\
**Category:** Programming\
**Created:** [September 26, 2023, 1:38pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359 "2023-09-26T13:38:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![stauberr](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@stauberr](https://forum.arduino.cc/u/stauberr)\
**Post date:** [September 26, 2023, 1:38pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/1 "2023-09-26T13:38:02Z")

</div>

Trying to understand how to return an array from a function. Can you help me understand what I should be doing to get a reference to the breakfast array in the code below?

```arduino
void setup() {
  Serial.begin(9600);
  char food[] = "Pizza";
  char* food_ptr = food;
  Serial.print("\nfood_ptr before the call: "); Serial.println(food_ptr); // prints "Pizza"
  food_ptr = foo();
  Serial.print("food_ptr after the call: "); Serial.println(food_ptr); // prints nothing
}
char* foo(){
  char breakfast[] = "bagel";
  return breakfast;
}

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [September 26, 2023, 1:39pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/2 "2023-09-26T13:39:39Z")

</div>

> [@stauberr](#):
>
> Trying to understand how to return an array from a function.

You can't return a a pointer to a variable that's local to the function. Define the array in the calling function and pass that into the called function (along with the array's size).

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [September 26, 2023, 1:43pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/3 "2023-09-26T13:43:58Z")

</div>

> [@gfvalvo](#):
>
> You can't return a a pointer to a variable that's local to the function

Oh yes you _can_ - but it is a very bad thing to do!! 😱 😱 😱

The pointer will continue to point at the memory location where the local variable _once was_ - but that memory will no longer be occupied by that variable!

You will create some very bewildering and hard-to-debug bugs this way!

Therefore, **you really do _not_ want to do this!**

Instead, as @gfvalvo said, pass-in a pointer to a buffer for your function to use.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 26, 2023, 2:48pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/4 "2023-09-26T14:48:06Z")

</div>

Alternatively (if that’s the need) return a pointer to constant and statically allocated memory

> [@stauberr](#):
>
> ```arduino
> const char * const bagel = "bagel"; // global
> 
> …
> 
> const char* const food() {
> return bagel;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [September 26, 2023, 2:53pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/5 "2023-09-26T14:53:30Z")

</div>

If you're just looking to have one from a list of food names, maybe have an array of strings, and return the index ... ?

```arduino
const char * foods[] = { "bagel", "Pizza" };

```

🤔

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [September 26, 2023, 4:52pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/7 "2023-09-26T16:52:44Z")

</div>

> [@Delta\_G](#):
>
> Not if you want your code to work reliably

Exactly! See the rest of that sentence - and, in fact, the rest of the post!

You _can_ - but you most certainly **should _not_!**

Which raises the question:

Is there a warning on this which can be enabled?

Something along the lines of, "returning pointer to auto object" ... ?  
🤔

EDIT

It seems the `-Wreturn-local-addr` does exist ...

EDIT 2

Current documentation only seems to mention an option to turn this _off_ - so it is, presumably, on by default?

> **[Warning Options (Using the GNU Compiler Collection (GCC))](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wno-return-local-addr)**
>
> Warning Options (Using the GNU Compiler Collection (GCC))

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [September 26, 2023, 5:00pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/8 "2023-09-26T17:00:04Z")

</div>

Well a pedant would take a minute to point out that you can return a pointer to a variable that's local to the function, if it is declared _static_.

And another minute to carefully make clear that he is not recommending that in case someone somehow thinks he is. 😑

It might could be just what you want to do. I'll ask the AI I have trained on 50000 lines of my own code to see if I ever happened to do that, and see if it was for any good reason.

a7

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [September 26, 2023, 5:05pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/9 "2023-09-26T17:05:33Z")

</div>

> [@alto777](#):
>
> carefully make clear that he is not recommending that in case someone somehow thinks he is. 😑

Is this not clear enough, then?

> [@awneil](#):
>
> Oh yes you _can_ - but it is a very bad thing to do!! 😱 😱 😱
> 
> The pointer will continue to point at the memory location where the local variable _once was_ - but that memory will no longer be occupied by that variable!
> 
> You will create some very bewildering and hard-to-debug bugs this way!
> 
> Therefore, **you really do _not_ want to do this!**

🙄

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [September 26, 2023, 5:06pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/10 "2023-09-26T17:06:50Z")

</div>

Normally I would pass a pointer to an array, have the function operate on that and have my array done if the function does not return a 0.

That is the way that most string.h functions work btw.

---

<div class="post-metadata">

**Author:** ![stauberr](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@stauberr](https://forum.arduino.cc/u/stauberr)\
**Post date:** [September 26, 2023, 5:49pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/11 "2023-09-26T17:49:51Z")

</div>

Thank you all very much for your responses. You've given me lots to think about and to look up.  
To alto777's point, I saw in a digitalOceans tutorial that you could return a pointer that points to a local static array, like this:

```arduino
char* foo(){
  static char breakfast[] = "bagel";
  return breakfast;
}

```

and that works (in this case anyway) but I think you are saying that is a bad idea. Can you explain why it's bad? Is it because I can't use that memory somewhere else now? Thank you again!  
EDIT: I changed static function to static array (thanks to awneil for pointing that out).

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [September 26, 2023, 5:54pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/12 "2023-09-26T17:54:30Z")

</div>

> [@stauberr](#):
>
> return a pointer to a local static function, like this

That's a static _array_ - not a function

> [@stauberr](#):
>
> that works

Yes: because the data is `static` - which is not what you showed in post #1.

Being static means that the data will always be present in the same location - so it is safe to return a pointer to it.

The reason why it's bad to return a pointer to a _non_-static local was explained in post #3

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [September 26, 2023, 5:57pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/13 "2023-09-26T17:57:33Z")

</div>

Being called a pedant before even finishing my post...

An other remark would be that returning a pointer to a string literal is safe too, e.g.,

```cpp
char const* foo(size_t const idx) {
  char const* msg[] {"zero", "one", "two"};
  return msg[idx];
}

```

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [September 26, 2023, 6:02pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/14 "2023-09-26T18:02:26Z")

</div>

It is common practice to send the array pointer and the size of the array as function arguments...  
That way you can prevent writing ouside array biundaries...  
Like with snprintf(char\* ptr, int size)...

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [September 26, 2023, 6:06pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/15 "2023-09-26T18:06:03Z")

</div>

> [@awneil](#):
>
> > [@stauberr](#):
> >
> > that works
> 
> Yes: because the data is `static` - which is not what you showed in post #1.
> 
> Being static means that the data will always be present in the same location - so it is safe to return a pointer to it.

But perhaps not reentrant-safe or recursive-safe.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [September 26, 2023, 6:19pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/16 "2023-09-26T18:19:59Z")

</div>

> [@stauberr](#):
>
> how to return an array from a function.

It turns out that this is a relatively difficult thing to do in C.  
The best solution is usually to define the array outside of the function, and pass the array (and its length) to the function as arguments:

```arduino
  char *food = "Pizza";
  Serial.print("\nfood before the call: "); Serial.println(food);
  foo(food, sizeof(food));
  Serial.print("food after the call: "); Serial.println(food);
}
char *foo(char* s, size_t len){
  strncpy(s, "bagel", len);
  return s;
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [September 26, 2023, 6:22pm UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/17 "2023-09-26T18:22:50Z")

</div>

> [@awneil](#):
>
> 🙄

No, perfectly clear. Your response was thorough and detailed, I was not trying to throw you under the bus.

> [@gfvalvo](#):
>
> But perhaps not reentrant-safe or recursive-safe.

There's a function _strtok_() that I believe uses static internal storage, and is thus is not reentrant.

Looking just now I see in the time I've been not paying attention, there is now _strtok_\__r_(), a modern version that is. Re-enterable.

a7

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [September 27, 2023, 7:01am UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/19 "2023-09-27T07:01:35Z")

</div>

Recursive code has the potential to grow the hell out of the stack.  
If the function always finishes before too many recursions it won't crash your sketch. How many too many is depends on space left.

Strange though, I write functions that get called a lot, finish a short step and return to be called again and again to achieve results I might use recursion to achieve in a larger memory computer.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 27, 2023, 7:31am UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/20 "2023-09-27T07:31:15Z")

</div>

> [@Delta\_G](#):
>
> and the
> 
> > [@awneil](#):
> >
> > Oh yes you _can_
> 
> which appears to be a retort

I think he was just playing in the words. You « can » do stupid things but you should not.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 27, 2023, 11:01am UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/22 "2023-09-27T11:01:48Z")

</div>

I did not read this the way you did

> [@awneil](#):
>
> Oh yes you _can_ - but it is a very bad thing  
> to do!! 😱 😱 😱

To me it was clearly reinforcing what you said in an humorous way something like « you are authorized to shoot yourself in the foot »… hopefully no one would do it…

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [September 27, 2023, 11:12am UTC](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359/23 "2023-09-27T11:12:58Z")

</div>

> [@alto777](#):
>
> Well a pedant would take a minute to point out that you can return a pointer to a variable that's local to the function, if it is declared _static_.
> 
> And another minute to carefully make clear that he is not recommending that in case someone somehow thinks he is. 😑

I've actually seen this in one of the real-time clock libraries, for returning text for the day of the week if I recall correctly.  
This can lead to hard to debug errors. If the function is called multiple times, and changes the data in the array each time, then you cannot save the pointer from a previous call and expect to have the old data, since every call to the function returns a pointer to the same array, which will contain only the data from the last call to the function.

[Next page](https://forum.arduino.cc/t/functions-returning-character-arrays/1172359.md?page=2)
