It has been some time I last handled strings and I have forgotten this. Things which http://www.cplusplus.com/doc/tutorial/ntcs/ says should work, do not. It is a bit strange
I tried to pass a string or character array by value or reference, and I am getting errors where I think I should not get. Besides, what is wrong with using a string variable?
string str3 = "trtty "; //This gives always an error "sketch_sep18b:5:
//error: 'string' does not name a type"
char str1[100]="fdfdfd";
char str2='l';
void setup()
{
Serial.begin(9600);
}
void loop()
{
gtcha (str1);
}
void gtcha (char &stra[]) //this does not work. The error "sketch_sep18b:5: error:
//declaration of 'stra' as array of references"
{
Serial.print(stra);
}
void gtchb (char strb[]) //this does work
{
Serial.print(strb);
}
LMI:
But I can't find any more info about strings as variables. So it is probably an object or a function call.
strings aren't variables. In the example you provided, you'll notice there is a #include at the top, to give you the object. In the Arduino environment, they are known as "Strings" and are a piss poor way of abstracting very simple concepts.
In C an array an a pointer to an object of the base type are interchangeable.
So if you declare an array of 100 characters:
char str1[100]="fdfdfd";
You can pass that string as a pointer to a string.
You usually define C functions to take a pointer to a string as a parameter:
void gtcha (char *stra) //This is the syntax you should use
{
Serial.print("stra = ");
Serial.println(stra);
}
LMI:
It has been some time I last handled strings and I have forgotten this. Things which http://www.cplusplus.com/doc/tutorial/ntcs/ says should work, do not. It is a bit strange
I tried to pass a string or character array by value or reference, and I am getting errors where I think I should not get. Besides, what is wrong with using a string variable?
//error: 'string' does not name a type"
char str1[100]="fdfdfd";
char str2='l';
void setup()
{
Serial.begin(9600);
}
void loop()
{
gtcha (str1);
}
void gtcha (char &stra[]) //this does not work. The error "sketch_sep18b:5: error:
//declaration of 'stra' as array of references"
{
Serial.print(stra);
}
void gtchb (char strb[]) //this does work
{
Serial.print(strb);
}
As pointed out the type name is wrong. C/C++ is case sensitive and names must match exactly: 'String'.
I tried to pass a string or character array by value or reference, and I am getting errors where I think I should not get.
void gtcha (char &stra[]) //this does not work. The error "sketch_sep18b:5: error:
//declaration of 'stra' as array of references"
{
Serial.print(stra);
}
You must use brackets to show the variable is a reference to an array, not an array of references. If you do, you must provide the length between the square brackets: