views:

1605

answers:

3

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 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?

+5  A: 

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
}
Jeffrey Hantin
Ok thanks!Being used to C#, I guess I assumed it'd be passing a reference to the function (eek).
amb9800
It (your orignal example) *does* return a reference, but to an object that ceases to exist after the function exits (so it becomes a dangling reference). C doesn't have in-built reference-counting / garbage-collection.
caf
+1  A: 

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;
}
caf
I don't think `sprintf` is part of the Arduino library -- the libs are brutally subsetted to fit in a 14kb maximum text segment size.
Jeffrey Hantin
Jeffrey Hantin: I've seen some examples that imply that it is, but doesn't have floating point support linked by default.
caf
A: 

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 */
pmg
Yeah, I had adapted the bracket syntax from an initialization I saw in a sample. Is there any way to assign multiple values in an array in one statement?
amb9800
There is no way to assign multiple array elements in one "simple" statement. But, as you're dealing with strings, you can use string functions, as in `strcpy(strOut, "00");` or `strcat(strOut, "00");` or `sprintf` ...
pmg