1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>18.1.7: Type Qualifiers
</title>
10 <link href=
"sx4fa.html" rev=precedes
>
11 <link href=
"sx4b.html" rel=precedes
>
12 <link href=
"sx4a.html" rev=subdocument
>
15 <H3>18.1.7: Type Qualifiers
</H3>
17 <p>[Type qualifiers are a fairly advanced feature
18 which not all programs need.
19 You may skip this section.]
20 </p><p>Any type can be qualified by the type qualifiers
21 <TT>const
</TT> or
<TT>volatile
</TT>.
22 Both of these were new with ANSI C,
28 Even in new code, you will see
<TT>const
</TT> fairly rarely,
29 and
<TT>volatile
</TT> even less often.
30 </p><p>In simple declarations,
31 the type qualifier is simply another keyword in the type name,
32 along with the basic type and the storage class.
37 extern volatile unsigned long int ul;
39 are all declarations involving type qualifiers.
40 </p><p>A
<TT>const
</TT> value is one you promise not to modify.
41 The compiler may therefore be able to make certain optimizations,
42 such as placing a
<TT>const
</TT>-qualified variable in read-only memory.
44 a
<TT>const
</TT>-qualified variable is not a true constant;
45 that is, it does not qualify
47 as a
<dfn>constant expression
</dfn>
48 which C requires in certain situations,
49 such as array dimensions,
55 and initializers for variables with static duration
56 (globals and
<TT>static
</TT> locals).
57 </p><p>A
<TT>volatile
</TT> value
59 is one that might change unexpectedly.
60 This situation generally only arises
61 when you're directly accessing special hardware registers,
62 usually when writing device drivers.
63 The compiler should not assume that a
<TT>volatile
</TT>-qualified variable
64 contains the last value that was written to it,
65 or that reading it again would yield the same result
66 that reading it last time did.
67 The compiler should therefore avoid making any optimizations
69 suppress seemingly-redundant
70 accesses to a
<TT>volatile
</TT>-qualified variable.
71 Examples of
<TT>volatile
</TT> locations would be a clock register
72 (which always gave an up-to-date time value each time you read it),
73 or a device control/status register,
74 which caused some peripheral device to perform an action
75 each time the register was written to.
76 </p><p>Type qualifiers become more interesting
77 (or at least more complicated or confusing)
78 when they modify pointer types.
79 The placement of the qualifier in a pointer declaration
80 determines whether it is the pointer itself,
81 or the location pointed to,
89 declare pointers to constant
<TT>int
</TT>s,
90 which means that although the pointers can be modified
91 (to point to different locations),
92 the locations pointed to
93 (that is,
<TT>*ci1
</TT> and
<TT>*ci2
</TT>)
94 can
<em>not
</em> be modified.
100 declares a pointer which cannot be modified
101 (it cannot be set to point anywhere else),
102 although the value it points to
105 </p><p>Pointers to constants
106 (such as
<TT>ci1
</TT> and
<TT>ci2
</TT> above)
107 have a particularly important use:
108 they can be used to document
110 pointer parameters which a function promises
<em>not
</em> to use
111 to modify locations in the caller.
112 </p><p>Normally, C uses pass-by-value.
113 A function receives copies of its arguments,
114 which means that it cannot modify any variables in the caller
115 (since copies of those variables were passed).
116 If a function receives a pointer, however
117 (including the pointer that results
118 when the caller seems to ``pass'' an array),
119 it
<em>can
</em> use that pointer
120 (more precisely, it can
121 use its copy of the pointer)
122 to modify locations in the caller.
123 Sometimes, this is just what is desired:
124 when the caller ``passes'' an array
125 which it wishes the function to fill in,
126 or when the function wants to return one or more values via pointers
127 rather than as the conventional return value,
128 the function's modification of locations in the caller
129 is deliberate and understood by the caller.
131 when a function receives a pointer argument for some other reason,
132 under circumstances in which the caller
133 might not want the function to use
134 the pointer to modify anything in the caller,
135 the caller might appreciate a guarantee
137 (within the function)
138 won't be used to modify anything.
139 To make that guarantee,
140 the function can declare
142 pointer-to-
<TT>const
</TT>.
144 our old friend
<TT>printf
</TT>
145 never scribbles on the string
147 as its format argument;
148 it merely uses it to decide what to print.
149 Therefore, the prototype for
<TT>printf
</TT> is
151 int printf(const char *fmt, ...)
153 where the
<TT>...
</TT> represents
154 <TT>printf
</TT>'s optional arguments.
155 If a caller writes something like
157 char mystring[] =
"Hello, world!\n";
162 knows, from
<TT>printf
</TT>'s prototype,
163 that
<TT>printf
</TT> won't be scribbling on
<TT>mystring
</TT>.
165 with that prototype for
<TT>printf
</TT> in scope,
166 the actual author of the
<TT>printf
</TT> code
168 accidentally write a (buggy) version
169 which inadvertently modified
170 the format argument--since
171 it's declared as
<TT>const char *
</TT>,
172 the compiler will complain
173 if any attempt is made to write to the location(s) it points to.
174 </p><p><TT>const
</TT> and
<TT>volatile
</TT> can also be used in combination.
175 Theoretically, it's possible to have a single variable which is both:
177 const volatile int x;
179 Also, both a pointer and what it points to can be qualified:
181 const char * const cpc;
184 as in several other situations,
185 C tends to assume type
<TT>int
</TT>,
186 so if you want to save a bit of typing,
199 <a href=
"sx4fa.html" rev=precedes
>prev
</a>
200 <a href=
"sx4b.html" rel=precedes
>next
</a>
201 <a href=
"sx4a.html" rev=subdocument
>up
</a>
202 <a href=
"top.html">top
</a>
205 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
206 //
<a href=
"copyright.html">Copyright
</a> 1996-
1999
207 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>