Problems with strings and &chars

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);    
}

//error: 'string' does not name a type"

The error message is telling you exactly what is wrong.

Is string a valid data type in C++ ?

In the link near the end, there is a line:
string answer2;

In the example.

int main ()
{
  char question1[] = "What is your name? ";
  string question2 = "Where do you live? ";
  char answer1 [80];
  string answer2;
  cout << question1;
  cin >> answer1;
  cout << question2;
  cin >> answer2;
  cout << "Hello, " << answer1;
  cout << " from " << answer2 << "!\n";
  return 0;

But I can't find any more info about strings as variables. So it is probably an object or a function call.

But what is wrong with this
void gtcha (char &stra[])

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?

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);   
}

string str3 = "trtty "; //This gives always an error "sketch_sep18b:5:

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:

void gtcha (char (&stra)[100]){
  Serial.print(stra);    
}

If the function needs to be used by multiple ( and different sized ) arrays, you can use a template to fill in the size:

template< unsigned N >
  void gtcha (char (&stra)[ N ]){
    Serial.print(stra);    
}

And if you have arrays with different data types, the template can handle that too:

template< typename T, unsigned N >
  void gtcha ( T (&stra)[ N ]){
    Serial.print(stra);    
}
1 Like

My thanks to you all.

I have again misremembered that "string" is name of variable type and not just an array of chars.

This is what I tried to do.

void gtcha (char (&stra)[100])

But it is too difficult to remember, so I'll just use

void gtcha (char *stra)
or
void gtchb (char strb[])

I have again misremembered that "string" is name of variable type and not just an array of chars.

What adds to the confusion is that in C# and other programming languages, string is a type.

Yes, like in VB, which I use a lot.