Hi,
I've read a lot of tutorials about pointers, However, I frequently see people use the & reference operator in a way that is not explained in any tutorials that I have read.
This is the kind of thing that confuses me:
It expects me to just pass a normal string like this: trim(mystring);Code:void trim(string& str) { string::size_type pos1 = str.find_first_not_of(' '); string::size_type pos2 = str.find_last_not_of(' '); str = str.substr(pos1 == string::npos ? 0 : pos1, pos2 == string::npos ? str.length() - 1 : pos2 - pos1 + 1); }
However, I don't really understand what is happening here, the function parameter seems to be asking for the address of a string, Although it will not accept this: trim(&mystring);
The way that I personally would write such a function is like this:
void trim(string* str)
{
}
and I would pass the pointer like this: trim(&mystring);
This makes sense to me, But I frequently see people using the & operator in function defintions, and I don't understand what it does.