blob: f9073230559a24c716374ba01983b740972af787 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
<html>
<head>
<title>Reference variables</title>
</head>
<body bgcolor="#dddddd">
<font color=brown>
<hr>
<center><h1>Reference Variables.</h1></center>
<hr>
<p>
Reference variables allow two variable names to address the
same memory location. The following example shows the
technique in its simplest form.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr>
<td>
<pre>
#include <iostream.h>
main()
{
int var1;
int &var2 = var1; // var2 is a reference variable.
var1 = 10;
cout << "var1 = " << var1 << endl;
cout << "var2 = " << var2 << endl;
}
</pre>
</td>
</tr>
</table>
</center>
<p>
Generally, you would not use a reference variable
in this way. Its more likely that they would be used
in function parameter lists to make the passing of
pointers more readable.
<p>
This gives C++ the ability to provide a
different approch to changing a variable from within a
function. Consider the two following programs.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr>
<td>
<pre>
#include <stdio.h>
void Square(<b>int *pVal</b>);
main()
{
int Number=10;
printf("Number is %d\n", Number);
Square(<b>&Number</b>);
printf("Number is %d\n", Number);
}
void Square(<b>int *pVal</b>)
{
<b>*pVal</b> *= <b>*pVal;</b>
printf("Number is %d\n", <b>*pVal</b>);
}
</pre>
</td>
<td>
<pre>
#include <stdio.h>
void Square(<b>int &Val</b>);
main()
{
int Number=10;
printf("Number is %d\n", Number);
Square(<b>Number</b>);
printf("Number is %d\n", Number);
}
void Square(<b>int &Val</b>)
{
<b>Val</b> *= <b>Val</b>;
printf("Number is %d\n", <b>Val</b>);
}
</pre>
</td>
</tr>
</table>
</center>
<p>
The program on the right should be clearer to read
because you do not need to worry about pointer
dereferencing.
<hr>
<h2>Examples:</h2>
<img src="../../GRAPHICS/computer.gif" alt="o">
<a href="..//EXAMPLES/refvar.cc">Example program.</a>
<p>
<font color=brown>
<img src="../../GRAPHICS/computer.gif" alt="o">
<a href="../../C/EXAMPLES/pointer_func.c">Example C program.</a>
</font>
<hr>
<h2>See Also:</h2>
</font>
<hr>
<font color=black>
<h2>C References</h2>
<p>
<img src="../../GRAPHICS/whiteball.gif" alt="o">
<a href="../../C/CONCEPT/data_types.html">data types</a>.
</font>
<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">Top</a>
</td><td width="25%">
<a href="../../C/master_index.html">Master Index</a>
</td><td width="25%">
<a href="../SYNTAX/keywords.html">Keywords</a>
</td><td width="25%">
<a href="../../C/FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<font color=brown>
<address>Martin Leslie
29-Sep-96</address><p>
</font>
</body>
</html>
|