next up previous
Next: Subprogram Linkage Up: Pointers, Arrays, and References Previous: Arrays

References

References are aliases, i.e. alternate names for another variable. They are not allocated any storage. They are required to be initialized when they are declared. Here is how to declare g to be a reference to a:

   double &g = a;
The type declaration double & specifies a reference to a double. Please notice that the ampersand is being used in a different context here. That is, it is being used in a declaration.
\framebox{\rm In a declaration the ampersand means \lq\lq reference''.}

Suppose, we now write

    g = -1.7;
Since g is just another name for a this means the same thing as
    a = -1.7;
Please note, we do not recommend writing your code this way! It would be very confusing to have two names for the same variable in the same block of code. So you will see references used primarily for passing values back and forth between programs and subprograms.



Carleton DeTar 2007-08-17