user52333: Still don't understand about char concatenation.
void setup() {

  testCounter(3);

}

void loop() {
  // put your main code here, to run repeatedly:
}

char testCounter(int countx){
  char a[10];
  strcpy(a, "COUNTER: ");
  strcat(a, printDigits(countx));
  return a;
}

char printDigits(int digits){
  char a[3];
  if(digits < 10) {
    strcpy(a, "0");
    sprintf(a,"%d",digits);
  } else {
    sprintf(a,"%d",digits);
  }
  return a;
}

Error:  invalid conversion from 'char' to 'const char' [-fpermissive]* in testCounter line:
strcat(a, printDigits(countx));

Don't understand this part of error.
printDigits function return char, so... strcat should be work. But why still error?
Please help...

user10794: char printDigits(int digits){

You are telling the compiler you will return one character.
 char a[3];

a is three characters. Thus the declaration doesn't match.

You could change the declaration to:
char * printDigits(int digits){

But even that would be bad because a would go out of scope after the function returns.

 char a[3];

That's a little short for putting an integer into. An integer might be 5 characters, plus possibly a minus sign, and you need a trailing "null" at the end. Seven bytes would be safer.

The simplest way of doing what you seem to be wanting is:
void setup() {
  Serial.begin (115200);
  testCounter(3);
}

void loop() {
}

char testCounter(int countx){
  char a[15];
  sprintf (a, "COUNTER: %02d", countx);
  Serial.println (a);
}


Also, here:
  if(digits < 10) {
    strcpy(a, "0");
    sprintf(a,"%d",digits);
  } else {

That won't work because you are putting "0" into a and then overwriting it with sprintf.


printDigits function return char, so... strcat should be work. But why still error?

Look at the prototype for strcat:
http://www.cplusplus.com/reference/cstring/strcat/
It expects char * not char. They are different things. One is a pointer, one isn't.

52333: <p>Still don't understand about char concatenation.</p>

<pre><code>void setup() {

  testCounter(3);

}

void loop() {
  // put your main code here, to run repeatedly:
}

char testCounter(int countx){
  char a[10];
  strcpy(a, "COUNTER: ");
  strcat(a, printDigits(countx));
  return a;
}

char printDigits(int digits){
  char a[3];
  if(digits &lt; 10) {
    strcpy(a, "0");
    sprintf(a,"%d",digits);
  } else {
    sprintf(a,"%d",digits);
  }
  return a;
}
</code></pre>

<p>Error:  <em>invalid conversion from 'char' to 'const char</em>' [-fpermissive]* in <code>testCounter</code> line:</p>

<pre><code>strcat(a, printDigits(countx));
</code></pre>

<p>Don't understand this part of error.</p>

<p><code>printDigits</code> function return char, so... <code>strcat</code> should be work. But why still error?</p>

<p>Please help...</p>
 10794: <pre class="lang-c++ prettyprint-override"><code>char printDigits(int digits){
</code></pre>

<p>You are telling the compiler you will return <strong>one</strong> character.</p>

<pre class="lang-c++ prettyprint-override"><code> char a[3];
</code></pre>

<p><code>a</code> is three characters. Thus the declaration doesn't match.</p>

<hr>

<p>You could change the declaration to:</p>

<pre class="lang-c++ prettyprint-override"><code>char * printDigits(int digits){
</code></pre>

<p>But even that would be bad because <code>a</code> would go out of scope after the function returns.</p>

<hr>

<pre class="lang-c++ prettyprint-override"><code> char a[3];
</code></pre>

<p>That's a little short for putting an integer into. An integer might be 5 characters, plus possibly a minus sign, and you need a trailing "null" at the end. Seven bytes would be safer.</p>

<hr>

<p>The simplest way of doing what you seem to be wanting is:</p>

<pre class="lang-c++ prettyprint-override"><code>void setup() {
  Serial.begin (115200);
  testCounter(3);
}

void loop() {
}

char testCounter(int countx){
  char a[15];
  sprintf (a, "COUNTER: %02d", countx);
  Serial.println (a);
}
</code></pre>

<hr>

<p>Also, here:</p>

<pre class="lang-c++ prettyprint-override"><code>  if(digits &lt; 10) {
    strcpy(a, "0");
    sprintf(a,"%d",digits);
  } else {
</code></pre>

<p>That won't work because you are putting "0" into <code>a</code> and then <strong>overwriting</strong> it with <code>sprintf</code>.</p>

<hr>

<blockquote>
  <p><code>printDigits</code> function return <code>char</code>, so... <code>strcat</code> should be work. But why still error?</p>
</blockquote>

<p>Look at the prototype for <code>strcat</code>:</p>

<p><a href="http://www.cplusplus.com/reference/cstring/strcat/" rel="nofollow noreferrer">http://www.cplusplus.com/reference/cstring/strcat/</a></p>

<p>It expects <code>char *</code> not <code>char</code>. They are different things. One is a pointer, one isn't.</p>
