I just finished the articles hosted on this site regarding pointers and I have to say that I'm a bit confused after reading it. It did clarify some parts for me but others I just questioned.
So in school we had to write a modularized program and without going into the specifics, I wrote a function that would take in all the values from user and using pointers, send them to the main().
So without all the extra stuff, code essentially is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int main ()
{
double principal (0), annual_rate (0), loan_term (0);
int count (0);
getUserInput ( count, principal, annual_rate, loan_term );
cin.get ();
return 0;
}
void getUserInput ( int count, double &principal, double &annual_interest, double &loan_term )
{
cin >> principal;
cin >> annual_interest;
cin >> loan_term;
return;
}
|
Although I used the ampersand (reference operator), it still modified the values in the main function like I wanted it to. However having read the article, my understanding of the reference operator is that it refers to the memory location not the value as showed with the example of Andy, Fred, and Ted. So even though I entered in a value (i.e principal), the value regardless of what it was should have remained constant in the main function because the reference operator is referring to where the memory is stored not what the actual value itself is.
However this was not the case and the value of principal, annual_interest, and loan_term were modified in the main function and I have no idea why.
I'm not sure if I'm making any sense and it seems to me that the logical thing to use would be an asterisk (*). Anyways any help is appreciated.
Article link btw:
http://www.cplusplus.com/doc/tutorial/pointers/