# Combining char arrays

**URL:** <https://forum.arduino.cc/t/combining-char-arrays/873062>\
**Category:** Programming\
**Created:** [June 10, 2021, 12:00pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062 "2021-06-10T12:00:02Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex3480](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@alex3480](https://forum.arduino.cc/u/alex3480)\
**Post date:** [June 10, 2021, 12:00pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/1 "2021-06-10T12:00:02Z")

</div>

Hello folks, I am currently programming an ESP32 and have trouble combining different char arrays.

I have these variables for example:

char mID[3] ="13";  
char shelly[] = "[http://192.168.0.1](http://192.168.0.1)";  
char relais[] = "/relay/0? HTTP/1.1";

and I want to combine these char array into one that looks like this:

"[http://192.168.0.113/relay/0](http://192.168.0.113/relay/0)? HTTP/1.1"

Im complety surprised that this is such a huge problem in C and really pissed off I allready tried many examples in google like strcat() or whatever but it does not work as intended.

Does anybody of you has an idea or working example?

(Im doing this so i dont have to change X lines when the IP changes and only have to change mID in code)

sincerly Alexander

---

<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:** [June 10, 2021, 12:03pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/2 "2021-06-10T12:03:26Z")

</div>

> [@alex3480](#):
>
> Im complety surprised that this is such a huge problem in C and really pissed off I allready tried many examples in google like strcat() or whatever but it does not work as intended.

strcat() and its variants do indeed work. So, it would be best if you posted the code where you attempted to use it. Someone here can then point out what you did wrong.

PS -- Post your code with Code Tags. If you don't know what that means, read the forum guidelines first.

---

<div class="post-metadata">

**Author:** ![alex3480](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@alex3480](https://forum.arduino.cc/u/alex3480)\
**Post date:** [June 10, 2021, 12:05pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/3 "2021-06-10T12:05:20Z")

</div>

char mID[3] ="17";  
char shelly[] = "[http://192.168.0.1](http://192.168.0.1)";  
char shellyOff[] = "[http://192.168.0.1](http://192.168.0.1)";  
char relais[] = "/relay/0? HTTP/1.1";  
char off[] = "/relay/0?turn=off HTTP/1.1";

strcat (shelly, mID);  
strcat (shelly, relais);  
strcat (shellyOff, mID);  
strcat (shellyOff, off);

output looks like this wtf?

shelly:  
13:52:41.621 -\> [http://192.168.0.17/relay/0](http://192.168.0.17/relay/0)? HTTP/1.1/relay/0?turn=off HTTP/1.1  
shellyOff  
13:52:41.714 -\> /relay/0? HTTP/1.1/relay/0?turn=off HTTP/1.1

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [June 10, 2021, 12:05pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/4 "2021-06-10T12:05:31Z")

</div>

consider following code producing

```cpp
http://192.168.0.1
/relay/0? HTTP/1.1
http://192.168.0.1/relay/0? HTTP/1.1

```

```cpp
char s [80];
const char shelly [] = "http://192.168.0.1";
const char relais [] = "/relay/0? HTTP/1.1";

void
setup ()
{
    Serial.begin (9600);

    strcpy (s, shelly);
    strcat (s, relais);

    Serial.println (shelly);
    Serial.println (relais);
    Serial.println (s);
}

void
loop ()
{
}

```

---

<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:** [June 10, 2021, 12:06pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/5 "2021-06-10T12:06:34Z")

</div>

@ [alex3480](https://forum.arduino.cc/u/alex3480), please read the forum guidelines (you should have done this before posting your question) and post your code properly with Code Tags.

BTW, you're writing beyond the end of the 'shelly' array into memory that you don't own.

---

<div class="post-metadata">

**Author:** ![alex3480](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@alex3480](https://forum.arduino.cc/u/alex3480)\
**Post date:** [June 10, 2021, 12:12pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/6 "2021-06-10T12:12:35Z")

</div>

I have to be honest I dont really seem to understand these functions but I think ts working now.

```cpp

char mID[3] ="17";
char shelly[128]; 
char shellyOff[128];
char ip[] = "http://192.168.0.1";
char relais[] = "/relay/0? HTTP/1.1";
char off[] = "/relay/0?turn=off HTTP/1.1";

  strcpy (shelly,ip);
  strcat (shelly,mID);
  strcat (shelly, relais);
  strcpy (shellyOff, ip);
  strcat (shellyOff, mID);
  strcat (shellyOff, off);

```

Is the memory problem fixed if I do it like this?

Thanks for your help 🙂

---

<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:** [June 10, 2021, 12:15pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/7 "2021-06-10T12:15:06Z")

</div>

Looks much better.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [June 10, 2021, 12:15pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/8 "2021-06-10T12:15:56Z")

</div>

strcat needs to append to something. could be a null string

---

<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:** [June 10, 2021, 12:28pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/9 "2021-06-10T12:28:11Z")

</div>

Global variables are initialized to 0. But this would certainly be preferable:

```cpp
char shelly[128] = {0};

```

---

<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:** [June 10, 2021, 12:33pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/10 "2021-06-10T12:33:50Z")

</div>

Or (to your point), even better:

```cpp
char shelly[128] = "";

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [June 10, 2021, 2:35pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/11 "2021-06-10T14:35:15Z")

</div>

> [@alex3480](#):
>
> Is the memory problem fixed if I do it like this?

Yes, as long as the resulting string is never longer than 127 bytes.

---

<div class="post-metadata">

**Author:** ![drmpf](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drmpf/32/1126438_2.png) [@drmpf](https://forum.arduino.cc/u/drmpf)\
**Post date:** [June 10, 2021, 2:52pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/12 "2021-06-10T14:52:52Z")

</div>

Avoid strcat() as it can easily overflow, particularly in your case where you are adding multiple strings.  
Instead use strlcat() which protects against overflows and crashes.  
see this sketch [strlcpy\_strlcat.ino](https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/strlcpy_strlcat.ino)  
for examples of how to use

Or for simple robust code use Arduino Strings and avoid memory problems

```cpp
char mID[3] ="17";
String shelly; 
String shellyOff;
char ip[] = "http://192.168.0.1";
char relais[] = "/relay/0? HTTP/1.1";
char off[] = "/relay/0?turn=off HTTP/1.1";

  shelly = ip;
  shelly += mID;
  shelly += relais;
  // then shelly.c_str() will return the char[] holding the result
char* result = shelly.c_str();

  shellyOff = ip;
  shellyOff += mID;
  shellyOff += off;
  // then shellyOff.c_str() will return the char[] holding the result
char* resultOff = shellyOff.c_str();

```

OR  
if you want to keep using char[]s like shelly[128] you can wrap them in a [SafeString](https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html) which protects against buffer overflows and has nice error messages.

```cpp
#include "SafeString.h"
char mID[3] ="17";
char shelly[128]; 
char shellyOff[128];
char ip[] = "http://192.168.0.1";
char relais[] = "/relay/0? HTTP/1.1";
char off[] = "/relay/0?turn=off HTTP/1.1";

  cSFA(sfShelly,shelly); // wrap shelly in a SafeString, which directly updates shelly[] char array
  sfShelly = ip;
  sfShelly += mID;
  sfShelly += relais;
  // then shelly[..] will be holding the result
  Serial.println(shelly);

  cSFA(sfShellyOff,shellyOff); // wrap shellyOff in a SafeString, which directly updates shellyOff[] char array
  sfShellyOff = ip;
  sfShellyOff += mID;
  sfShellyOff += off;
   // then shellyOff[..] will be holding the result
  Serial.println(shellyOff);

```

---

<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:** [October 8, 2021, 9:47pm UTC](https://forum.arduino.cc/t/combining-char-arrays/873062/14 "2021-10-08T21:47:47Z")

</div>

Closed on October 8, 2021, 9:47pm UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
