I have a problem receiving the serial input as the following message appears during compilation: incompatible types in the assignment of int to char[20] . Can you guys help me out by suggesting changes to the code below to get rid of this error ?
int c=0,a=0,d=0,i=0,e=0;
int b;
char str[10];
void setup(){
Serial.println("Enter a string: ");
Serial.begin(9600);
suggest you need to spend a little time understanding arrays. When you reference an array using only its name, you are asking for where that variable is stored in memory (i.e., its lvalue), not what's stored in the array. Think of a bucket. You have defined str to be a 10-byte bucket. Let's assume that the compiler stores str at memory address 2000. Your first statement above assigns 2000 into b. If you want to see what's inside the bucket (its rvalue), you need to "dereference" the array. One way to do this is:
b = str[0];*
The other way is to create an appropriate pointer variable and initialize it to point to the array, as in:
_ char *ptr;_
ptr = str; // Initialize the pointer to point to the bucket (lvalue)*
_ if (*ptr >= '0' && *ptr <0 '9'); // Use indirection to peek inside the bucket (rvalue)_
Two more things: Avoid "magic numbers" in your code, like 48 and 57. Notice how much easier it is to read the if test. Second, your syntax for the multiple conditions is incorrect. It is normally written as presented above.