2

Here’s the code:

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

    double lat = 1111.11;
    double lon = 4444.44;

    const byte len{10};

    char sLat[len];
    char sLon[len];

    dtostrf(lat, len, 2, sLat);
    Serial.println(sLat);

    Serial.println("---");

    dtostrf(lon, len, 2, sLon);
    Serial.println(sLon);
    Serial.println(sLat); // <-- prints nothing!

    Serial.println("---");

    dtostrf(lat, len, 2, sLat);
    Serial.println(sLat);
    Serial.println(sLon); // <-- prints nothing!
}

void loop()
{
}

Here’s the output:

   1111.11
---
   4444.44

---
   1111.11
   4444.44   1111.11

As you see. Every next call for dtostrf ruins the previous results, in a weird way.

What’s the problem?

1 Answer 1

2

If len is the text array size, you need space there for the terminating '\0'. So the second paramater to dtostrf has to be smaller than len.

1
  • Yeah, of course, your're right! Thanks a lot! I fixed declaration: char sLat[len+1]
    – zhekaus
    Oct 24, 2019 at 16:12

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.