I have a char array of length 10. It gets filled with an unpredictable number of chars, usually up to 6.

char charDummy[10] = "";

When i concatenate the char array with another, I get spaces, which are unwanted in later parsing.

char postStr[300] = "";
strcat(postStr, "&field1=");

How do I trim a char array directly?

Workaround: loop thru the array and copy valid characters to a new char array with perfect number of rows. Due to memory constraints, I need to be parcimonious with the amount of variables created.

1 Answer 1


Your problem is that dtostrf() right-aligns the value in the space allotted to it. The simple answer is to tell it to left-align instead:

Conversion is done in the format "[-]d.ddd". The minimum field width of the output string (including the possible '.' and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. width is signed value, negative for left adjustment.


So, just make your width negative and it will left align.

  • @tony gil: The string terminator (ASCII 0) has nothing to do with NULL and is never referred to as NULL. NULL is a completely different, unrelated thing in C and C++. Jun 1, 2019 at 0:04

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.