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>Pointer Increment/Decrement (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Pointer Increment/Decrement (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Pointer Increment/Decrement (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="Pointers.html" rel="up" title="Pointers">
|
---|
33 | <link href="Pointer-Arithmetic-Drawbacks.html" rel="next" title="Pointer Arithmetic Drawbacks">
|
---|
34 | <link href="Pointer-Arithmetic-Low-Level.html" rel="prev" title="Pointer Arithmetic Low Level">
|
---|
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="Pointer-Increment_002fDecrement"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Pointer-Arithmetic-Drawbacks.html" accesskey="n" rel="next">Pointer Arithmetic Drawbacks</a>, Previous: <a href="Pointer-Arithmetic-Low-Level.html" accesskey="p" rel="prev">Pointer Arithmetic Low Level</a>, Up: <a href="Pointers.html" accesskey="u" rel="up">Pointers</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="Pointer-Increment-and-Decrement"></span><h3 class="section">14.13 Pointer Increment and Decrement</h3>
|
---|
64 | <span id="index-pointer-increment-and-decrement"></span>
|
---|
65 | <span id="index-incrementing-pointers"></span>
|
---|
66 | <span id="index-decrementing-pointers"></span>
|
---|
67 |
|
---|
68 | <p>The ‘<samp>++</samp>’ operator adds 1 to a variable. We have seen it for
|
---|
69 | integers (see <a href="Increment_002fDecrement.html">Increment/Decrement</a>), but it works for pointers too.
|
---|
70 | For instance, suppose we have a series of positive integers,
|
---|
71 | terminated by a zero, and we want to add them all up.
|
---|
72 | </p>
|
---|
73 | <div class="example">
|
---|
74 | <pre class="example">int
|
---|
75 | sum_array_till_0 (int *p)
|
---|
76 | {
|
---|
77 | int sum = 0;
|
---|
78 |
|
---|
79 | for (;;)
|
---|
80 | {
|
---|
81 | /* <span class="roman">Fetch the next integer.</span> */
|
---|
82 | int next = *p++;
|
---|
83 | /* <span class="roman">Exit the loop if it’s 0.</span> */
|
---|
84 | if (next == 0)
|
---|
85 | break;
|
---|
86 | /* <span class="roman">Add it into running total.</span> */
|
---|
87 | sum += next;
|
---|
88 | }
|
---|
89 |
|
---|
90 | return sum;
|
---|
91 | }
|
---|
92 | </pre></div>
|
---|
93 |
|
---|
94 | <p>The statement ‘<samp>break;</samp>’ will be explained further on (see <a href="break-Statement.html">break Statement</a>). Used in this way, it immediately exits the surrounding
|
---|
95 | <code>for</code> statement.
|
---|
96 | </p>
|
---|
97 | <p><code>*p++</code> parses as <code>*(p++)</code>, because a postfix operator always
|
---|
98 | takes precedence over a prefix operator. Therefore, it dereferences
|
---|
99 | <code>p</code>, and increments <code>p</code> afterwards. Incrementing a variable
|
---|
100 | means adding 1 to it, as in <code>p = p + 1</code>. Since <code>p</code> is a
|
---|
101 | pointer, adding 1 to it advances it by the width of the datum it
|
---|
102 | points to—in this case, one <code>int</code>. Therefore, each iteration
|
---|
103 | of the loop picks up the next integer from the series and puts it into
|
---|
104 | <code>next</code>.
|
---|
105 | </p>
|
---|
106 | <p>This <code>for</code>-loop has no initialization expression since <code>p</code>
|
---|
107 | and <code>sum</code> are already initialized, it has no end-test since the
|
---|
108 | ‘<samp>break;</samp>’ statement will exit it, and needs no expression to
|
---|
109 | advance it since that’s done within the loop by incrementing <code>p</code>
|
---|
110 | and <code>sum</code>. Thus, those three expressions after <code>for</code> are
|
---|
111 | left empty.
|
---|
112 | </p>
|
---|
113 | <p>Another way to write this function is by keeping the parameter value unchanged
|
---|
114 | and using indexing to access the integers in the table.
|
---|
115 | </p>
|
---|
116 | <div class="example">
|
---|
117 | <pre class="example">int
|
---|
118 | sum_array_till_0_indexing (int *p)
|
---|
119 | {
|
---|
120 | int i;
|
---|
121 | int sum = 0;
|
---|
122 |
|
---|
123 | for (i = 0; ; i++)
|
---|
124 | {
|
---|
125 | /* <span class="roman">Fetch the next integer.</span> */
|
---|
126 | int next = p[i];
|
---|
127 | /* <span class="roman">Exit the loop if it’s 0.</span> */
|
---|
128 | if (next == 0)
|
---|
129 | break;
|
---|
130 | /* <span class="roman">Add it into running total.</span> */
|
---|
131 | sum += next;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return sum;
|
---|
135 | }
|
---|
136 | </pre></div>
|
---|
137 |
|
---|
138 | <p>In this program, instead of advancing <code>p</code>, we advance <code>i</code>
|
---|
139 | and add it to <code>p</code>. (Recall that <code>p[i]</code> means <code>*(p +
|
---|
140 | i)</code>.) Either way, it uses the same address to get the next integer.
|
---|
141 | </p>
|
---|
142 | <p>It makes no difference in this program whether we write <code>i++</code> or
|
---|
143 | <code>++i</code>, because the value is not used. All that matters is the
|
---|
144 | effect, to increment <code>i</code>.
|
---|
145 | </p>
|
---|
146 | <p>The ‘<samp>--</samp>’ operator also works on pointers; it can be used
|
---|
147 | to scan backwards through an array, like this:
|
---|
148 | </p>
|
---|
149 | <div class="example">
|
---|
150 | <pre class="example">int
|
---|
151 | after_last_nonzero (int *p, int len)
|
---|
152 | {
|
---|
153 | /* <span class="roman">Set up <code>q</code> to point just after the last array element.</span> */
|
---|
154 | int *q = p + len;
|
---|
155 |
|
---|
156 | while (q != p)
|
---|
157 | /* <span class="roman">Step <code>q</code> back until it reaches a nonzero element.</span> */
|
---|
158 | if (*--q != 0)
|
---|
159 | /* <span class="roman">Return the index of the element after that nonzero.</span> */
|
---|
160 | return q - p + 1;
|
---|
161 |
|
---|
162 | return 0;
|
---|
163 | }
|
---|
164 | </pre></div>
|
---|
165 |
|
---|
166 | <p>That function returns the length of the nonzero part of the
|
---|
167 | array specified by its arguments; that is, the index of the
|
---|
168 | first zero of the run of zeros at the end.
|
---|
169 | </p>
|
---|
170 | <hr>
|
---|
171 | <div class="header">
|
---|
172 | <p>
|
---|
173 | Next: <a href="Pointer-Arithmetic-Drawbacks.html" accesskey="n" rel="next">Pointer Arithmetic Drawbacks</a>, Previous: <a href="Pointer-Arithmetic-Low-Level.html" accesskey="p" rel="prev">Pointer Arithmetic Low Level</a>, Up: <a href="Pointers.html" accesskey="u" rel="up">Pointers</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>
|
---|
174 | </div>
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | </body>
|
---|
179 | </html>
|
---|