Pages: [1]   Go Down
Author Topic: Problem with Char() function  (Read 988 times)
0 Members and 1 Guest are viewing this topic.
Turku
Offline Offline
Full Member
***
Karma: 1
Posts: 224
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am playing with strcat and char functions. I am trying to get a result something like " haly=44" in a text variable "plaa".
Code:
int first = 0;    
int  inByte;
char ppp;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available() > 0) {
    inByte = Serial.read();
    first++;
    char plaa[]="haly=";
    strcat(plaa," hh");
    ppp=char(first);
    strcat(plaa,ppp);
    Serial.println(plaa);
  }
}
But I get error:
 In function 'void loop()':
error: invalid conversion from 'char' to 'const char*'

In char() documentation in references, there is nothing special mentioned. I have tried many ways to declare the char variable, but nothing seems to help. Is something missing or do I have to make this a full C++ program with libraries and so on.  And eat Arduino's memory.
« Last Edit: June 01, 2010, 04:53:27 pm by LMI1 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49320
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have declared a character array, plaa, of fixed size, with an initial value:
Code:
char plaa[]="haly=";

Now, even though there is no room in the array, you are trying to add more characters to the end:
Code:
strcat(plaa," hh");

Here, you are casting an integer to a character:
Code:
ppp=char(first);

This is not converting the integer to it's string representation. In other words, if first was 48, ppp would not be "48". Instead, ppp would be '0'. (Or something close...)

Now, you are trying to append that character to the array in which there is no room:
Code:
strcat(plaa,ppp);

The compiler does not like this code, because the 2nd argument for the strcat function is supposed to be a string (a null-terminated array of characters), not a single character.

You need to declare your initial array, plaa, to have a fixed, larger size:
Code:
char plaa[40];

Then, you can initialize it:
Code:
plaa[0] = '\0';
strcat(plaa, "haly=");

Now, if you want to convert first to a string:
Code:
char firstStg[8];
itoa(first, firstStg, 10); // 3rd argument is base, not size (10 is decimal)

Now, you can concatenate the strings:
Code:
strcat(plaa, firstStg);
Logged

Turku
Offline Offline
Full Member
***
Karma: 1
Posts: 224
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code works, thanks.

I wonder is the char() help page a little short or misleading.

Here is the whole program if somebody happens to need it.

Code:
char plaa[100];  // don't build more than 99 chars to this!
int val = 0;                    // variable for reading the pin status
char firstStg[8];
void setup()
{
  Serial.begin(9600);
}
void loop()
{
val++;
plaa[0]='\0';
itoa(val, firstStg, 10); // 3rd argument is base, not size (10 is decimal)
strcpy(plaa, "haly=");
strcat(plaa, firstStg);
Serial.println(plaa);
delay(1500);
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49320
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I wonder is the char() help page a little short or misleading.

Not if you understand what a char variable is, and the range of values it can hold, and the differences between char variables, arrays of chars, and strings. The char() page has a link to the char page which has a link to the array page.
Logged

Pages: [1]   Go Up
Jump to: