English 中文(简体)
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 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] =   ;
}

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] =   ;

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 ,   }; /* ok, initialization */
strOut = { a ,  b ,  c ,   }; /* wrong, cannot assign to array */




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签