Arduino String Formatting Issue
I'm making an Arduino-powered clock, and in the process, I'm trying to format integers into two-digit formatted strings for the time read-out (e.g. 1 into "01").
The following gives me "error: expected primary-expression before '{' token":
char * formatTimeDigits (int num) {
char strOut[3] = "00";
if (num < 10) {
strOut = {'0', char(num)};
}
else {
strOut = char(num);
}
return strOut;
}
I'm trying to use it as follows:
void serialOutput12() {
printWeekday(weekday); // picks the开发者_如何学C right word to print for the weekday
Serial.print(", "); // a comma after the weekday
Serial.print(hour12, DEC); // the hour, sent to the screen in decimal format
Serial.print(":"); // a colon between the hour and the minute
Serial.print(formatTimeDigits(minute)); // the minute
Serial.print(":"); // a colon between the minute and the second
Serial.print(formatTimeDigits(second)); // the second
}
Any ideas as to what I'm missing here?
The curly-brace syntax is valid for initial declaration of a variable, but not for assignment after the fact.
Also, you are returning a pointer to an automatic variable, which is no longer validly allocated once returned (and will be smashed by the next call, such as to print
). You need to do something like this:
void formatTimeDigits(char strOut[3], int num)
{
strOut[0] = '0' + (num / 10);
strOut[1] = '0' + (num % 10);
strOut[2] = '\0';
}
void serialOutput12()
{
char strOut[3]; // the allocation is in this stack frame, not formatTimeDigits
printWeekday(weekday); // picks the right word to print for the weekday
Serial.print(", "); // a comma after the weekday
Serial.print(hour12, DEC); // the hour, sent to the screen in decimal format
Serial.print(":"); // a colon between the hour and the minute
formatTimeDigits(strOut, minute);
Serial.print(strOut); // the minute
Serial.print(":"); // a colon between the minute and the second
formatTimeDigits(strOut, second);
Serial.print(strOut); // the second
}
In C, you can't directly set an array's contents with the =
assignment operator (you can initialise an array, but that's a different thing, even though it looks similar).
Additionally:
- It doesn't sound like the Wiring
char(value)
function/operator does what you want; and - If you want to return a pointer to that
strOut
array, you will have to make it have static storage duration.
The simple way to do what you want is sprintf
:
char * formatTimeDigits (int num)
{
static char strOut[3];
if (num >= 0 && num < 100) {
sprintf(strOut, "%02d", num);
} else {
strcpy(strOut, "XX");
}
return strOut;
}
A couple things:
- You cannot assign to an array:
strOut = {'0', (char)num};
- You return the address of an object that will cease to exist right after the return statement.
For the first problem, assign to array elements:
strOut[0] = '0';
strOut[1] = num;
strOut[2] = '\0';
For the 2nd problem, the solution is a bit more complicated. The best would be to pass a destination string to the FormatTimeDigits()
function and let the caller worry about it.
FormatTimeDigits(char *destination, int num); /* prototype */
FormatTimeDigits(char *destination, size_t size, int num); /* or another prototype */
Still another point on the 1st item: you may have seen something similiar in an initialization. That's different than assignment, and it allows a similar looking construct as assignment.
char strOut[] = {'a', 'b', 'c', '\0'}; /* ok, initialization */
strOut = {'a', 'b', 'c', '\0'}; /* wrong, cannot assign to array */
精彩评论