source: public/doc/gnu-c/const.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.0 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <a href="volatile.html" accesskey="n" rel="next">volatile</a>, Up: <a href="Type-Qualifiers.html" accesskey="u" rel="up">Type Qualifiers</a> &nbsp; [<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 &ldquo;constant&rdquo; by writing <code>const</code> in
69front of the declaration. This says to treat any assignment to that
70variable as an error. It may also permit some compiler
71optimizations&mdash;for instance, to fetch the value only once to satisfy
72multiple 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>
79but 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
86as enumeration constants, and they are not limited to integers. The
87constantness of the variable propagates into pointers, too.
88</p>
89<p>A pointer type can specify that the <em>target</em> is constant. For
90example, the pointer type <code>const double *</code> stands for a pointer
91to a constant <code>double</code>. That&rsquo;s the typethat results from taking
92the address of <code>pi</code>. Such a pointer can&rsquo;t be dereferenced in the
93left side of an assignment.
94</p>
95<div class="example">
96<pre class="example">*(&amp;pi) = 3.0; /* <span class="roman">Error!</span> */
97</pre></div>
98
99<p>Nonconstant pointers can be converted automatically to constant
100pointers, but not vice versa. For instance,
101</p>
102<div class="example">
103<pre class="example">const double *cptr;
104double *ptr;
105
106cptr = &amp;pi; /* <span class="roman">Valid.</span> */
107cptr = ptr; /* <span class="roman">Valid.</span> */
108ptr = cptr; /* <span class="roman">Error!</span> */
109ptr = &amp;pi; /* <span class="roman">Error!</span> */
110</pre></div>
111
112<p>This is not an ironclad protection against modifying the value. You
113can 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> */
117ptr = (double *)&amp;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
121won&rsquo;t modify the data structure whose address is passed to it. Here&rsquo;s
122an example:
123</p>
124<div class="example">
125<pre class="example">int
126string_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
136function 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
140char *</code>.
141</p>
142<hr>
143<div class="header">
144<p>
145Next: <a href="volatile.html" accesskey="n" rel="next">volatile</a>, Up: <a href="Type-Qualifiers.html" accesskey="u" rel="up">Type Qualifiers</a> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.