# Help: char trouble

Hello,

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[9];
strcpy(a, "COUNTER: ");
strcat(a, printDigits(countx));
return a;
}

char* printDigits(int digits){
char* a[3];
if(digits < 10) {
a = strcat("0",(char*) digits);
} else {
a = (char*) digits;
}
return a;
}
``````

Error: cannot convert ‘char**’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’ in line:

``````strcpy(a, "COUNTER: ");
``````

Edit testCounter function:

``````char* testCounter(int countx){
char* a[9] = "COUNTER: ";
strcat(a, printDigits(countx));
return a;
}
``````

Also error: array must be initialized with a brace-enclosed initializer in line:

``````char* a[9] = "COUNTER: ";
``````

How to make it works?

This statement

`````` char* a[9]
``````

is creating an array of 9 elements. Each element is of type char *, not char. If you actually want to copy things into the array 'a' you need to declare it as `char a[9]` which means you have 9 elements of type 'char' so you can then copy things into it

``````strcpy(a, "COUNTER: ");
``````

blh64:
This statement

`````` char* a[9]
``````

is creating an array of 9 elements. Each element is of type char *, not char. If you actually want to copy things into the array ‘a’ you need to declare it as

``````char a[9]
``````

which means you have 9 elements of type ‘char’ so you can then copy things into it

``````strcpy(a, "COUNTER: ");
``````

Edit the code:

``````void setup() {

testCounter(3);

}

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

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

char* printDigits(int digits){
char* a[3];
if(digits < 10) {
a = strcat("0",(char*) digits);
} else {
a = (char*) digits;
}
return a;
}
``````

Error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive] in testCounter function line:

``````return a;
``````

All else aside, don't try to return a pointer to a local variable unless it's defined as 'static'.

gfvalvo:
All else aside, don’t try to return a pointer to a local variable unless it’s defined as ‘static’.

Edit the code… again

``````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) {
a = strcat("0",(char) digits);
} else {
a = (char) digits;
}
return a;
}
``````

Now… error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] in printDigits function line:

``````a = strcat("0",(char) digits);
``````

Plese help…

``````a = strcat("0",(char) digits);
``````

Have you read the reference page for strcat? Does this seem like a good idea?

Also, you're still trying to return a pointer to a local variable.

gfvalvo:

``````a = strcat("0",(char) digits);
``````

Have you read the reference page for strcat? Does this seem like a good idea?

Also, you’re still trying to return a pointer to a local variable.

gfvalvo…

Thanks a lot for the guidance.

Edit the 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 < 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 function line:

``````strcat(a, printDigits(countx));
``````

I don’t understand this part of error.

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

The code is a train wreck. Problems:

• Your function 'printDigits()' is claimed to return a char but it's trying to return 'a'. As I've told you multiple times, 'a' is a pointer to a local variable. Don't try to return it. The array it points to ceases to exist after the function returns.

• Your function 'printDigits()' is claimed to return a char but you're trying to use its returned value in a call to 'strcat()' that requires a char *.

• Your function 'testCounter ()' is claimed to return a char but it's trying to return 'a'. Again, this is a pointer to a local variable, don't try to return it.

wieb:
Edit the code… again

``````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) {
a = strcat(“0”,(char) digits);
} else {
a = (char) digits;
}
return a;
}

``````

Now... error: _invalid conversion from 'char' to 'const char*' [-fpermissive]_ in printDigits function line:

``````

a = strcat(“0”,(char) digits);

``````

Plese help...
``````

You’ve defined the character array a[] to have 10 elements, so after the string copy:

strcpy(a, "COUNTER: ");

you have a[] looking like this:

-----±----±----±----±----±----±----±----±—±----+
C | O | U | N | T | E | R | : | | ‘\0’ | // strings end with a null, ‘\0’
-----±----±----±----±----±----±----±----±—±----+

In the next statement:

• strcat(a, printDigits(countx));*

you try to concatenate a digit character ‘3’ to the a[] array, but you’ve used up all of the elements you asked for. Who knows what you trashed in memory with that statement.

Finally, unless you use the static keyword in the definition of any variable defined within a function, it disappears when program control executes the last statement in the function. This means that a[] “disappears” and you cannot return it from the call to testCounter().

Also, what happens if you write the string copy call as:

strcpy(a, (const char*) "COUNTER: ");