# passing char Array back from function with pointer

**URL:** <https://forum.arduino.cc/t/passing-char-array-back-from-function-with-pointer/582041>\
**Category:** Programming\
**Created:** [March 26, 2019, 6:52am UTC](https://forum.arduino.cc/t/passing-char-array-back-from-function-with-pointer/582041 "2019-03-26T06:52:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ard24](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@ard24](https://forum.arduino.cc/u/ard24)\
**Post date:** [March 26, 2019, 6:52am UTC](https://forum.arduino.cc/t/passing-char-array-back-from-function-with-pointer/582041/1 "2019-03-26T06:52:07Z")

</div>

Dear All,  
I run into trouble by passing a char array from a function back to where it was called with a pointer. It seems to me like a bug. I try to explain the behavior:

This is my first sketch:

```cpp
char ArrayMain[7];
char * ptArrayMain;

char* test()
{
 char array[] = "HELLO\0";
 return array;
}

void setup()
{
 Serial.begin(9600);
 Serial.println("Start:");
 
 ptArrayMain = test();
 Serial.print(ptArrayMain);

}

void loop()
{

}

```

This gives me this result on Serial Monitor:

```cpp
Port closed
Uploading to I/O board
Opening port
Port open
Start:
AVR I

```

In front of "AVR I" there are two unknown chars printed. I would not expect this result.

By printing the value of array[] inside the function test() it gives me the expected result:  
Code:

```cpp
char ArrayMain[7];
char * ptArrayMain;

char* test()
{
 char array[] = "HELLO\0";
 Serial.println(array);
 
 return array;
}

void setup()
{
 Serial.begin(9600);
 Serial.println("Start:");
 
 ptArrayMain = test();
 Serial.print(ptArrayMain);

}

void loop()
{

}

```

Result:

```cpp
Port closed
Uploading to I/O board
Opening port
Port open
Start:
HALLO
HALLO

```

This is a very strange behavior for me. It looks like a bug or? Could anybody explan this to me? Would there a workarround?

I want to prevent using Serial.print just to be able to pass back with char pointer.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [March 26, 2019, 7:18am UTC](https://forum.arduino.cc/t/passing-char-array-back-from-function-with-pointer/582041/2 "2019-03-26T07:18:49Z")

</div>

Your _array_ variable in the _test_ function goes out-of-scope and the data is no longer guaranteed (can be overwritten).

You can make _array_ static or global.

Note:  
You don't need to add the terminating '\0' after "HELLO"; it's there because you use a c-string.

---

<div class="post-metadata">

**Author:** ![ard24](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@ard24](https://forum.arduino.cc/u/ard24)\
**Post date:** [March 26, 2019, 7:43am UTC](https://forum.arduino.cc/t/passing-char-array-back-from-function-with-pointer/582041/3 "2019-03-26T07:43:08Z")

</div>

Hi sterretje,  
thank you very much for your reply.

ohhh, I think the penny droped 😉

This means actually it makes no sense to pass an arry or pointer after function call. I don't want to have stored the array inside test() permanently.

I just remembered some other example where the array to work on is passed by pointer like following example. Can you please have a look in this if this is the correct way of doing it?

```cpp
char ArrayMain[7];
char * ptArrayMain = &ArrayMain[0];

test(char* buffer, int sizeofbuffer)
{
 char array[] = "HALLO\0";
 
 strncpy(buffer, &array[0], sizeofbuffer);
 //Serial.println(array);
}

void setup()
{
 Serial.begin(9600);
 Serial.println("Start:");
 //Serial.println((long) &ptArrayMain, DEC);
 
 test(ptArrayMain, sizeof(ArrayMain)/sizeof(ArrayMain[0]));
 
 Serial.print(ptArrayMain);

}

void loop()
{

}

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [March 26, 2019, 8:59am UTC](https://forum.arduino.cc/t/passing-char-array-back-from-function-with-pointer/582041/4 "2019-03-26T08:59:13Z")

</div>

_&ArrayMain[0]_ is the same as _ArrayMain_ 😉

Further I think it should work; just try it 🙂

---

<div class="post-metadata">

**Author:** ![ard24](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@ard24](https://forum.arduino.cc/u/ard24)\
**Post date:** [March 26, 2019, 12:43pm UTC](https://forum.arduino.cc/t/passing-char-array-back-from-function-with-pointer/582041/5 "2019-03-26T12:43:00Z")

</div>

Yes, looks good!

Thank you very much!

Seems like I learned a lot for today 😉

---

<div class="post-metadata">

**Author:** ![Jiggy-Ninja](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Jiggy-Ninja](https://forum.arduino.cc/u/Jiggy-Ninja)\
**Post date:** [March 26, 2019, 3:07pm UTC](https://forum.arduino.cc/t/passing-char-array-back-from-function-with-pointer/582041/6 "2019-03-26T15:07:18Z")

</div>

What is the point of the ptArrayMain variable? What is it contributing to your sketch? Something like that looks completely unnecessary. Array types are already trivially convertable into pointers, you don't need to make a separate pointer variable to work with them.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:43pm UTC](https://forum.arduino.cc/t/passing-char-array-back-from-function-with-pointer/582041/7 "2021-05-05T23:43:24Z")

</div>


