Problem using strcat function.

I also have a weird problem related to strcat. I have two global char array for example char str1[12] and char str2[10], now in my code some data are given from user and should be stored into these arrays. This is the process:

  • get data1 and data2

in the code:

strcat(str1, "HB");
strcat(str1, data1);
Serial.println(str1); //HB1234556

strcat(str2,"UR");
strcat(str2, data2);
Serial.println(str2); //UR10302493
Serial.println(str1); //HB1234556UR10302493

But the problem is that when I put data1 into str1 and I print it, its value is correct, and also value of str2 is OK, but when I print value of str1 after str2 value is set, I see that str2 is concatenated at the end of str1!! What can be the problem??

You're probably running off the end of one array.
If you posted your code, we could tell for sure.

TheMemberFormerlyKnownAsAWOL:
You're probably running off the end of one array.
If you posted your code, we could tell for sure.

this is part of my code:

char deviceid[14];
char mobile_id[13];

setup
{
....
}

loop
{
if ( command_type == 10)
{

WifiSettings = 1;
const char *router_ssid = root["data_val1"]; //from parsing json data
const char *router_pass = root["data_val2"];
const char *user_id = root["user_id"];

if (user_id != NULL)
{
strcpy(mobile_id, "UR");
strcat(mobile_id, user_id);
}

Serial.print("userid: ");
Serial.println(mobile_id); //here mobileid is OK

char MACAddr[17];
strcpy(MACAddr, WiFi.macAddress().c_str());

String device_id = "HB";

int k = 2;
for (int i = 0; i < 17; i++)
{
if (i != k)
{
device_id += MACAddr*;*

  • }*
  • else*
  • k += 3;*
  • }*
  • if (device_id.length() > 1 && device_id.length() <= 26)*
  • strcpy(deviceid, device_id.c_str());*
  • Serial.println(deviceid); //deviceid is OK*
  • Serial.println(mobile_id); //but here mobileid is concatenated with deviceid!!*
  • // JSON response*
  • String s = "HTTP/1.1 200 OK\r\n";*
  • s += "Content-Type: application/json\r\n\r\n";*
  • s += "{"c_type":";*
  • s += String(command_type);*
  • s += ","Status":"";*
  • s += String(Status);*
  • s += "","hub_id":"";*
  • s += device_id;*
  • s += ""}\r\n";*
  • s += "\n";*
  • WaitResponse = 1;*
  • client.print(s);*
  • delay(1);*
  • client.stop();*
    }
    }

...and that's why we ask you to use code tags when posting code.