Hi,Gill
I think your example is really confusing for me at beginning, you know , I am also quite new
to C++, but I love it and would like to spend time on it.
The first thing you should know is whether your function GetInput is pass-by-value or pass-by-refernce.(If you don't know what's the difference between them, look it up in google....)
In fact,your example gave me a deeper understanding of them,thanks.I wrote a test program as below:
#include <iostream.h>
#include <cstring>
void GetInput(char *name)
{
name="Irene";
}
void GetInput2(char **name)
{*name="Jessie";
}
int main()
{
char *word = "Peter";
cout<<"original: "<<word<<endl;
word ="Ken";
cout<<"middle : "<<word<<endl;
GetInput(word);
cout<<"finally: "<<word<<endl;
GetInput2(&word);
cout<<"finally: "<<word<<endl;
}
The output is :
original: Peter
middle : Ken
finally: Ken
finally: Jessie
The reason is acually simple , just because GetInput use pass-by-value whilst GetInput2 use pass-by-reference.
Although the formal argument in GetInput is a pointer,it isn't the address of the variable word(word itself is a pointer!).So, in GetInput , you acually only modified the copy version of word.
But why can CIN work in your fucntion? I am not very sure, I think that's because the copy version is only a temporary copy,if cin put our input things in this copy one, it will soon disapear,so it have to store it in the real version...anyway, it's about the internal facility
of CIN....
|