blob: 3ca368379fd7d6732ac454b08223d05155a4337b (
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
<head>
<title>The const keyword.</title>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>The const keyword.</h1>
</center>
<hr>
The const keyword is used to create a read only variable. Once initialised,
the value of the variable cannot be changed but can be used just like
any other variable.
<h2>const syntax</h2>
<p>
<center>
<table bgcolor=ivory width="80%" border=1>
<tr><td>
<pre>
main()
{
const float pi = 3.14;
}
</pre>
</td></tr>
</table>
</center>
<p>
The const keyword is used as a qualifier to the following data types -
int float char double struct.
<p>
<center>
<table bgcolor=ivory width="80%" border=1>
<tr><td>
<pre>
const int degrees = 360;
const float pi = 3.14;
const char quit = 'q';
</pre>
</td></tr>
</table>
</center>
<p>
<h2>const and pointers.</h2>
Consider the following example.
<p>
<center>
<table bgcolor=ivory width="80%" border=1>
<tr><td>
<pre>
void Func(const char *Str);
main()
{
char *Word;
Word = (char *) malloc(20);
strcpy(Word, "Sulphate");
Func(Word);
}
void Func(const char *Str)
{
}
</pre>
</td></tr>
</table>
</center>
<p>
The <code>const char *Str</code> tells the compiler that the DATA the
pointer points too is <code>const</code>. This means, Str can be changed
within Func, but *Str cannot. As a copy of the pointer is passed to Func,
any changes made to Str are not seen by main....
<p>
<center>
<table bgcolor=ivory width="80%" border=1>
<tr><td>
<pre>
--------
| Str | Value can be changed
-----|--
|
|
V
--------
| *Str | Read Only - Cannot be changed.
--------
</pre>
</td></tr>
</table>
</center>
<p>
<h2>Geeky Stuff</h2>
It is still possible to change the contents of a 'const' variable.
<a href=../EXAMPLES/const2.c>Consider this program</a> it creates
a const variable and then changes its value by accessing the data by
another name.
<p>
I am not sure if this applies to all compilers, but,
you can place the 'const' after the datatype, for example:
<p>
<center>
<table bgcolor=ivory width="80%" border=1>
<tr><td>
<pre>
int const degrees = 360;
float const pi = 3.14;
char const quit = 'q';
</pre>
</pre>
</td></tr>
</table>
</center>
<p>
are all valid in 'gcc'.
<p>
<h2>More Geeky Stuff</h2>
What would you expect these to do?
<p>
<center>
<table bgcolor=ivory width="80%" border=1>
<tr><td>
<pre>
main()
{
const char * const Variable1;
char const * const Variable2;
};
</pre>
</td></tr>
</table>
</center>
<p>
These both make the pointer and the data read only.
Here are a few more examples.
<p>
<center>
<table bgcolor=ivory width="80%" border=1>
<tr><td>
<pre>
const int Var; /* Var is constant */
int const Var; /* Ditto */
int * const Var; /* The pointer is constant,
* the data its self can change. */
const int * Var; /* Var can not be changed. */
</pre>
</td></tr>
</table>
</center>
<p>
<hr>
<h2>See Also.</h2>
<img src="../../GRAPHICS/whiteball.gif" alt="o">
<a href="../SYNTAX/define_preprocessor.html">#define preprocessor</a>
<p>
<font color=brown>
<img src="../../GRAPHICS/whiteball.gif" alt="o">
C++ version of
<a href="../../CPLUSPLUS/SYNTAX/const.html">const</a>
</font>
<hr>
<h2>An Example.</h2>
<img src="../../GRAPHICS/computer.gif" alt="o">
<a href=../EXAMPLES/const.c> const</a> example.
<p>
<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="../master_index.html"> Master Index</a>
</td><td width="25%">
<a href="keywords.html"> Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm"> Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie
</address><p>
</body>
</html>
|