user1499: Some days ago i started a thread.
concatenation of non constant character array with a sting
I have a different question but on the same nature (string and chars)
what i want is one variable (string or char) that will hold a standard text and the value of gps coordinates latitude and longitude
The format is this: "latitude/longitude: 30.111111 20.111111"
What i have is a char that holds the standard text and the latitude: "Latitude/Longitude: 30.111111"
I also have another char that holds the second value (longitude)
The chars were generated with dtostrf():
char clat[10 + 20 + 1] = "Latitude/Longitude: ";
char clng[10 + 1];
dtostrf(gps.location.lat(), 10, 6, clat+20);
dtostrf(gps.location.lng(), 10, 6, clng);

I can also make the chars strings:
string1 = String(clat);

What have i tried:
1.Assigning clng to clat directly via dtostrf()
char clat[10 + 20 + 10 + 1] = "Latitude/Longitude: ";
dtostrf(gps.location.lng(), 10, 6, clng+31);

It didnt work and got unexpected output

making clat a string and assign clng via for loop
string1 = String(clat);
    for (int i=0; i<10; i++)
    string1[i+32]=clng[i];

This doesnt append it
I run out of ideas here.
Any help from more experienced guys?

user1499: solved it. was simple actually. dont know how to do it with chars, but with strings there is the addition operator.
So i converted both to string and used the + operator.
char clat[10 + 20 + 1] = "Latitude/Longitude: ";
char clng[10 + 1];
String string1, string2, finalstr;

and inside the loop:
dtostrf(gps.location.lat(), 10, 6, clat+20);
dtostrf(gps.location.lng(), 10, 6, clng);
string1 = String(clat);
string2 = String(clng);
finalstr = string1 + string2;

Maybe that was too much work?
If you have any smarter alternatives, please provide!

user220: One thing I noticed is you do not need the + in:
char clat[10 + 20 + 1] = "Latitude/Longitude: ";

You can just do this:
char clat[31] = "Latitude/Longitude: ";

A much simpler way to do this is this:
Variables to initialize:
char subfinal[40] = "Latitude/Longitude: ";
String final = ""; //You don't need this, you could always use `String(subfinal)` instead

The code inside a function:
dtostrf(gps.location.lat(), 10, 6, subfinal+10);
subfinal[29] = " ";
dtostrf(gps.location.lng(), 10, 6, subfinal+30);
final = String(subfinal);

I haven't tested it, so let me know if it doesn't work.

1499: <p>Some days ago i started a thread.
<a href="https://arduino.stackexchange.com/questions/3023/concatenation-of-non-constant-character-array-with-a-sting">concatenation of non constant character array with a sting</a></p>

<p>I have a different question but on the same nature (string and chars)</p>

<p>what i want is one variable (string or char) that will hold a standard text and the value of gps coordinates latitude and longitude</p>

<p>The format is this: "latitude/longitude: 30.111111 20.111111"</p>

<p>What i have is a char that holds the standard text and the latitude: "Latitude/Longitude: 30.111111"</p>

<p>I also have another char that holds the second value (longitude)</p>

<p>The chars were generated with dtostrf():</p>

<pre><code>char clat[10 + 20 + 1] = "Latitude/Longitude: ";
char clng[10 + 1];
dtostrf(gps.location.lat(), 10, 6, clat+20);
dtostrf(gps.location.lng(), 10, 6, clng);
</code></pre>

<p>I can also make the chars strings:</p>

<pre><code>string1 = String(clat);
</code></pre>

<p>What have i tried:
1.Assigning clng to clat directly via dtostrf()</p>

<pre><code>char clat[10 + 20 + 10 + 1] = "Latitude/Longitude: ";
dtostrf(gps.location.lng(), 10, 6, clng+31);
</code></pre>

<p>It didnt work and got unexpected output</p>

<ol>
<li><p>making clat a string and assign clng via for loop</p>

<p>string1 = String(clat);
    for (int i=0; i&lt;10; i++)
    string1[i+32]=clng[i];</p></li>
</ol>

<p>This doesnt append it</p>

<p>I run out of ideas here.
Any help from more experienced guys?</p>
 1499: <p>solved it. was simple actually. dont know how to do it with chars, but with strings there is the addition operator.
So i converted both to string and used the + operator.</p>

<pre><code>char clat[10 + 20 + 1] = "Latitude/Longitude: ";
char clng[10 + 1];
String string1, string2, finalstr;
</code></pre>

<p>and inside the loop:</p>

<pre><code>dtostrf(gps.location.lat(), 10, 6, clat+20);
dtostrf(gps.location.lng(), 10, 6, clng);
string1 = String(clat);
string2 = String(clng);
finalstr = string1 + string2;
</code></pre>

<p>Maybe that was too much work?
If you have any smarter alternatives, please provide!</p>
 220: <p>One thing I noticed is you do not need the <code>+</code> in:</p>

<pre><code>char clat[10 + 20 + 1] = "Latitude/Longitude: ";
</code></pre>

<p>You can just do this:</p>

<pre><code>char clat[31] = "Latitude/Longitude: ";
</code></pre>

<p><strong>A much simpler way to do this is this:</strong></p>

<p>Variables to initialize:</p>

<pre><code>char subfinal[40] = "Latitude/Longitude: ";
String final = ""; //You don't need this, you could always use `String(subfinal)` instead
</code></pre>

<p>The code inside a function:</p>

<pre><code>dtostrf(gps.location.lat(), 10, 6, subfinal+10);
subfinal[29] = " ";
dtostrf(gps.location.lng(), 10, 6, subfinal+30);
final = String(subfinal);
</code></pre>

<p>I haven't tested it, so let me know if it doesn't work.</p>
