Reference Operator

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/
Last edited on
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.


When you use the &operator like that in a function parameter list, it behaves differently. What it's doing is something called "passing by reference". When you pass by reference, you have access to the original value, rather than a copy. This can speed things up when you want to avoid making copies of large objects, such as images. This also means that whatever you do to the variable (passed by reference) within that function is permanent.

When you pass by reference, you don't need to use a * operator to get from a memory address to a value.
So if I have a function some_function ( some_parameter ), when I call on this function using the pass-by-reference method (i.e. some_function ( variable ) ), however the value is modified inside will be reflected back on the original function?

Is this the reason why my function here can modify the array without the use of deference or reference operator?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void array_flip ( float arg [], int size)
  {

    float temp_array[size];
    int i, k = 0;

    for ( i = 0; i < size; i++)
      {
          temp_array[i] = arg[i];
      }

    for(i=(size-1);i>=0;i--)
      {
        arg[i] = temp_array[k];
        k++;
      }

    return;
  }


It will flip the original array without the use of reference/deference operators.
Last edited on
That's because arrays are just pointers to their first element when passed to a function. The [] operator does dereferencing.

EDIT: Fixed a typo.
Last edited on
Topic archived. No new replies allowed.