1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>const (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="const (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="const (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Type-Qualifiers.html" rel="up" title="Type Qualifiers">
|
---|
33 | <link href="volatile.html" rel="next" title="volatile">
|
---|
34 | <link href="Type-Qualifiers.html" rel="prev" title="Type Qualifiers">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="const"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="volatile.html" accesskey="n" rel="next">volatile</a>, Up: <a href="Type-Qualifiers.html" accesskey="u" rel="up">Type Qualifiers</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="const-Variables-and-Fields"></span><h3 class="section">21.1 <code>const</code> Variables and Fields</h3>
|
---|
64 | <span id="index-const-variables-and-fields"></span>
|
---|
65 | <span id="index-variables_002c-const"></span>
|
---|
66 | <span id="index-const"></span>
|
---|
67 |
|
---|
68 | <p>You can mark a variable as “constant” by writing <code>const</code> in
|
---|
69 | front of the declaration. This says to treat any assignment to that
|
---|
70 | variable as an error. It may also permit some compiler
|
---|
71 | optimizations—for instance, to fetch the value only once to satisfy
|
---|
72 | multiple references to it. The construct looks like this:
|
---|
73 | </p>
|
---|
74 | <div class="example">
|
---|
75 | <pre class="example">const double pi = 3.14159;
|
---|
76 | </pre></div>
|
---|
77 |
|
---|
78 | <p>After this definition, the code can use the variable <code>pi</code>
|
---|
79 | but cannot assign a different value to it.
|
---|
80 | </p>
|
---|
81 | <div class="example">
|
---|
82 | <pre class="example">pi = 3.0; /* <span class="roman">Error!</span> */
|
---|
83 | </pre></div>
|
---|
84 |
|
---|
85 | <p>Simple variables that are constant can be used for the same purposes
|
---|
86 | as enumeration constants, and they are not limited to integers. The
|
---|
87 | constantness of the variable propagates into pointers, too.
|
---|
88 | </p>
|
---|
89 | <p>A pointer type can specify that the <em>target</em> is constant. For
|
---|
90 | example, the pointer type <code>const double *</code> stands for a pointer
|
---|
91 | to a constant <code>double</code>. That’s the typethat results from taking
|
---|
92 | the address of <code>pi</code>. Such a pointer can’t be dereferenced in the
|
---|
93 | left side of an assignment.
|
---|
94 | </p>
|
---|
95 | <div class="example">
|
---|
96 | <pre class="example">*(&pi) = 3.0; /* <span class="roman">Error!</span> */
|
---|
97 | </pre></div>
|
---|
98 |
|
---|
99 | <p>Nonconstant pointers can be converted automatically to constant
|
---|
100 | pointers, but not vice versa. For instance,
|
---|
101 | </p>
|
---|
102 | <div class="example">
|
---|
103 | <pre class="example">const double *cptr;
|
---|
104 | double *ptr;
|
---|
105 |
|
---|
106 | cptr = &pi; /* <span class="roman">Valid.</span> */
|
---|
107 | cptr = ptr; /* <span class="roman">Valid.</span> */
|
---|
108 | ptr = cptr; /* <span class="roman">Error!</span> */
|
---|
109 | ptr = &pi; /* <span class="roman">Error!</span> */
|
---|
110 | </pre></div>
|
---|
111 |
|
---|
112 | <p>This is not an ironclad protection against modifying the value. You
|
---|
113 | can always cast the constant pointer to a nonconstant pointer type:
|
---|
114 | </p>
|
---|
115 | <div class="example">
|
---|
116 | <pre class="example">ptr = (double *)cptr; /* <span class="roman">Valid.</span> */
|
---|
117 | ptr = (double *)&pi; /* <span class="roman">Valid.</span> */
|
---|
118 | </pre></div>
|
---|
119 |
|
---|
120 | <p>However, <code>const</code> provides a way to show that a certain function
|
---|
121 | won’t modify the data structure whose address is passed to it. Here’s
|
---|
122 | an example:
|
---|
123 | </p>
|
---|
124 | <div class="example">
|
---|
125 | <pre class="example">int
|
---|
126 | string_length (const char *string)
|
---|
127 | {
|
---|
128 | int count = 0;
|
---|
129 | while (*string++)
|
---|
130 | count++;
|
---|
131 | return count;
|
---|
132 | }
|
---|
133 | </pre></div>
|
---|
134 |
|
---|
135 | <p>Using <code>const char *</code> for the parameter is a way of saying this
|
---|
136 | function never modifies the memory of the string itself.
|
---|
137 | </p>
|
---|
138 | <p>In calling <code>string_length</code>, you can specify an ordinary
|
---|
139 | <code>char *</code> since that can be converted automatically to <code>const
|
---|
140 | char *</code>.
|
---|
141 | </p>
|
---|
142 | <hr>
|
---|
143 | <div class="header">
|
---|
144 | <p>
|
---|
145 | Next: <a href="volatile.html" accesskey="n" rel="next">volatile</a>, Up: <a href="Type-Qualifiers.html" accesskey="u" rel="up">Type Qualifiers</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
146 | </div>
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|
150 | </body>
|
---|
151 | </html>
|
---|