I have an Arduino Duemilanove.

I read this page on string concatenation. Yet, it didn't include my case.

I have a non constant character array, that later gets a value.

char clat[11];
dtostrf(gps.location.lat(), 11, 6, clat);

I want to output this:

Latitude: 20.653425

So basically I want a final string that contains: "Latitude:" + the value of the nonconstant array.

Is it possible?

2 Answers 2

char clat[10+11+1] = "Latitude: ";
dtostrf(gps.location.lat(), 11, 6, clat+10);

Since you've named the buffer 'clat' I'm assuming it will always be used for latitude, so we've pre-initialized its size and contents, and we know where to begin the latitude value.

By the way, don't forget to size string buffers one more byte than the length of the maximum length string you intend to put in it.

Update 6/23/14:

From your other question and your comment to this answer, I'll revise my suggestion to this. The ..printf() functions are particularly easy to use, saves you (mis)counting buffer positions, and don't add much lot more code than the Serial() object already includes:

char outbuf[40+1];      // wider than nec, easier than counting
char clat[15+1]; clong[15+1]; // workspaces

sprintf(outbuf, "Latitude/Longitude: %s %s",
    dtostrf(gps.location.lat(), 10, 6, temp),
    dtostrf(gps.location.lng(), 10, 6, temp) );

This works because dtostrf() returns the address of the buffer you gave it; sprintf then takes that string (two of them in this case) when building your final output string.

The printf() functions are often deprecated for embedded systems. They're large (but see my comment about that above); and they're non-reentrant, so don't do that! In these low-end systems we don't often multi-thread anyway - just be aware of that if you do. And even if you have to remove them later and go back to printing piece by piece, they can save you a lot of headaches during development.

Here's another way that avoids both printf and the 32 bytes of work buffers:

char outbuf[40+1];

 strcpy(outbuf, "Latitude/Longitude: ");
 dtostrf(gps.location.lat(), 12, 6, outbuf+strlen(outbuf));
 dtostrf(gps.location.lng(), 12, 6, outbuf+strlen(outbuf));

We can't initialize outbuf unless it is 1) static, and 2) not going to be used more than once, since its initial conditions won't be the same the second time around. I've increased the field-widths of dostrf() to be sure the outputs of the won't be run-together for large values.

  • totally worked! But i have an extra question. Imagine i have two values a latitude and longitude. So char clat[11],clon[11]; And dtostrf(gps.location.lat(), 10, 6, clat); dtostrf(gps.location.lon(), 10, 6, clat); And i want a final string that is "Latitude/Longitude: 30.111111 20.111111". How do i do that? I tried char final[20 + 30m + 1] = "Latitude/Longitude: "; dtostrf(gps.location.lat(), 10, 6, clat+10); dtostrf(gps.location.lng(), 10, 6, clng+32); and other permutations of the numbers but none of these work. I get "Latitude/L 30.000000" Message is cut and 2nd value doesnt appera at all! Jun 22, 2014 at 15:24
  • For "Latitude/Longitude: 30.111111 20.111111" literally, you would need char cfinal[39+1] = "Latitude/Longitude: "; dtostrf(gps.location.lat(), 11, 6, cfinal+20); dtostrf(gps.location.lat(), 11, 6, cfinal+30);. I'd probably make the array a little longer and make the dtostrf() fields a little wider. Lat/longs could go to -180.0.... If you plan for it now, your maintenance at a later time will easier.
    – JRobert
    Jun 23, 2014 at 12:55
  • I just read your other question on this topic. See my edited answer above - should help you with both questions.
    – JRobert
    Jun 23, 2014 at 13:44

It's possible and very easy to do. You can convert a character array to a string object with the String(char_array_name_here) function.

To do this you can just do this:

String output = "Latitude: " + String(clat);  //Make sure to add a space so it's not "Latitude:20.653425"

You can do this with any string, just remove the String() part because you don't need to convert a string to a string!

One thing to note: you can just do this code if you're outputting it to the serial console:

Serial.println("Latitude: " + clat);

Summary of edits: found a function I was searching for yesterday but couldn't find. Much simpler

Other links:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.