# string2char question

**URL:** <https://forum.arduino.cc/t/string2char-question/526799>\
**Category:** Programming\
**Created:** [May 18, 2018, 1:30pm UTC](https://forum.arduino.cc/t/string2char-question/526799 "2018-05-18T13:30:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayhoung](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@ayhoung](https://forum.arduino.cc/u/ayhoung)\
**Post date:** [May 18, 2018, 1:30pm UTC](https://forum.arduino.cc/t/string2char-question/526799/1 "2018-05-18T13:30:33Z")

</div>

Hi all! Newbie here.

Quite new to c / c++. I have a question about the below code:

```cpp
char* string2char(String command){
    if (command.length() != 0) {
        char *p = const_cast<char*>(command.c_str());
        return p;
    }
}

void setup() {}

void loop() {
        String string1 = "Bob";
	char *string1Char = string2char(string1);
	String string2 = "Ross";
	char *string2Char = string2char(string2);
	Serial.println(string1Char);
	Serial.println(string2Char);
}

```

This basically outputs repeatedly:

```cpp
Ross
Ross

```

I understand I'm failing to grasp the concept of how pointers are working here - would someone be able to explain it? And how would I alter this so that it could show:

```cpp
Bob
Ross

```

---

<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 18, 2018, 3:49pm UTC](https://forum.arduino.cc/t/string2char-question/526799/2 "2018-05-18T15:49:31Z")

</div>

p goes out of scope when the function ends. Anything you do with it after it goes out of scope you do at your own peril.

---

<div class="post-metadata">

**Author:** ![christop](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christop/32/1013044_2.png) [@christop](https://forum.arduino.cc/u/christop)\
**Post date:** [May 18, 2018, 4:03pm UTC](https://forum.arduino.cc/t/string2char-question/526799/3 "2018-05-18T16:03:59Z")

</div>

String's c\_str() function returns a pointer to the String's internal buffer. It doesn't make a copy.

What's happening here is that you're passing each String to string2char by copy to its "command" parameter (which is a local variable to string2char), and the lifetime of "command" ends when string2char returns. Any pointer into that object is invalid after that point.

One simple change should fix that:

```cpp
char* string2char(String& command){

```

There's another bug, though: What if the command String is empty? string2char doesn't return a value in that case. You need to return a valid pointer (even NULL would be fine, if that's your intention) in all cases.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 18, 2018, 4:07pm UTC](https://forum.arduino.cc/t/string2char-question/526799/4 "2018-05-18T16:07:49Z")

</div>

> This basically outputs repeatedly:

Without a Serial.begin() I bet it doesn't

Please explain what you are trying to do

---

<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:11pm UTC](https://forum.arduino.cc/t/string2char-question/526799/5 "2021-05-05T23:11:47Z")

</div>

Closed on May 5, 2021, 11:11pm UTC


