strcat problem?

my program is:

#include<string.h>
char array1[9]="One ";
char array2[9]="Two ";

void setup() 
{
 Serial.begin(19200);
}

void loop() 
{
  strcat(array1,array2);
   Serial.println(array1); 
   delay(1000);
}

i got the output as:

One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two

but i'm expecting output as:

One Two
One Two
One Two
.
.
.
so on

why this not happened?

Because your array1 is constantly being incremented with array2, the only reason it goes back to "One Two" is because array size is just 9 chars. If it was larger u'd get results like "One Two Two Two Two" etc. To accomplish your point you need array1 to return to its original state after printing it to Serial.

Or, probably better, make a temporary array you will strcat these values into and then print it to Serial:

void loop() 
{
  char array3[9]={0};
  strcat(array3,array1);
  strcat(array3,array2);
  Serial.println(array3); 
  delay(1000);
}

ok,
Sir i have used String data type but is giving very random data.
i'm appending string like-

msg_gsm="OK" + String('^')+String("ON")+String('^')+String(str.substring(25,32))+String('^')+String(analogRead(A0), DEC);

it is giving very random results and arduino serial port hangs why this is so?

how to avoid hanging problem?

Sir,

#include<string.h>
char array1[9]="One ";
char array2[9]="Two ";

void setup() 
{
 Serial.begin(19200);
}

void loop() 
{
  char array3[9]={0};
  strcat(array1,array2);
  strcpy(array3,array1);
   Serial.println(array3); 
   delay(1000);
}

this also gives-

One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two 
One Two 
One Two Two

why the output not as-
One Two
One Two
.
.
.

Very random errors i'm getting?

#include<string.h>
char array1[5]="One ";
char array2[5]="Two ";

void setup() 
{
 Serial.begin(19200);
}

void loop() 
{
  char array3[10]={0};
  strcat(array1,array2);
  strcpy(array3,array1);
   Serial.println(array3); 
   delay(1000);
}

This is working fine?
i thing size of third array should be more than addition of first two.

but can you tell me about my second questions?
it is not solved.String data type gives very random data type?

Try the code I gave you, it does work. As i said, you cant strcat(array1,array2) without returning array1 to its previous state after you printed it to serial

And as for:

String('^')//rest of casts

The correct cast syntax is:

(String)'^'//rest of casts

Remember that "ON" and str.substring are already Strings so you dont need to cast them

Once you have concatenated array2 with array1 you never return array2 to its original contents. Could that be a problem ?

yes sir,it's working.

but using String Data type can i add so many strings?
where it is stored in memory?how can i clear string?

is clearing string is like this:

String a="Ankush";
Serial.println(a);
a="";// can i use this to clear string

where can i get the String data type related data except from learning?

Hello UKHeliBob,

I think that's not a problem.array2 is already defined as global.Sir, post #5 program working fine.

ankushg989:
Hello UKHeliBob,

I think that's not a problem.array2 is already defined as global.Sir, post #5 program working fine.

This code is "fine"?

#include<string.h>
char array1[5]="One ";
char array2[5]="Two ";

void setup() 
{
 Serial.begin(19200);
}

void loop() 
{
  char array3[10]={0};
  strcat(array1,array2);
  strcpy(array3,array1);
   Serial.println(array3); 
   delay(1000);
}

Isn't there a bit of a problem with that first "strcat"?

Its stored in SRAM. The amount of strings you add to each other doesnt have any limit, just the total length of strings, so it doesnt exceed SRAM capacity.

Hello UKHeliBob Sir,

No, it's working fine.what type of error you expecting?

I'm not expecting an error (the code is syntactically correct), and it may look like it is working, but the code is unsafe.

AWOL Sir,

no problem.what problem did you expect?

Hello Disty,
How can i clear String data types string?
where can i read total description on String data type?
where should i find the correct syntax?

but i'm expecting output as:

then, your "expectation" is wrong.

void loop() 
{
  char array3[9]={0};
  strcat(array1,array2);
  strcpy(array3,array1);
   Serial.println(array3); 
   delay(1000);
}

Are you slow ? Did you follow the example that was provided to you in reply #1

Let me try to explain it to you, very simply.

The first time loop( ) executes, it adds the chars from array2, onto the end of array1. THIS CHANGES ARRAY1. The second time through the loop( ), array2 gets added onto the end of array1 AGAIN. Onto the end of the MODIFIED array1 which was already changed in the first iteration of the loop.

That's why you see "One Two Two" on the output. You have also written to memory beyond the 9 characters which you allocated, which will eventually cause a problem.

Hello michinyon,

i'm getingouput as-

One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two 
One Two

same i want.

If that's what you want, then fine.
Just don't expect that technique to work next time, or even the same code with a different compiler.

You do, but after all I guess you dont need a program that just prints "One Two", but you are going to modify it/move to other program to accomplish the real goal. And thats where will it, most likely, crash. For example change array1 size from [5] to its original size [9], or anything else and you will have wrong output.