source: public/doc/gnu-c/Pointer-Increment_002fDecrement.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: 7.4 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>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<!--
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="Pointer-Increment_002fDecrement"></span><div class="header">
59<p>
60Next: <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> &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="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 &lsquo;<samp>++</samp>&rsquo; operator adds 1 to a variable. We have seen it for
69integers (see <a href="Increment_002fDecrement.html">Increment/Decrement</a>), but it works for pointers too.
70For instance, suppose we have a series of positive integers,
71terminated by a zero, and we want to add them all up.
72</p>
73<div class="example">
74<pre class="example">int
75sum_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&rsquo;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 &lsquo;<samp>break;</samp>&rsquo; 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
98takes precedence over a prefix operator. Therefore, it dereferences
99<code>p</code>, and increments <code>p</code> afterwards. Incrementing a variable
100means adding 1 to it, as in <code>p = p + 1</code>. Since <code>p</code> is a
101pointer, adding 1 to it advances it by the width of the datum it
102points to&mdash;in this case, one <code>int</code>. Therefore, each iteration
103of 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>
107and <code>sum</code> are already initialized, it has no end-test since the
108&lsquo;<samp>break;</samp>&rsquo; statement will exit it, and needs no expression to
109advance it since that&rsquo;s done within the loop by incrementing <code>p</code>
110and <code>sum</code>. Thus, those three expressions after <code>for</code> are
111left empty.
112</p>
113<p>Another way to write this function is by keeping the parameter value unchanged
114and using indexing to access the integers in the table.
115</p>
116<div class="example">
117<pre class="example">int
118sum_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&rsquo;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>
139and add it to <code>p</code>. (Recall that <code>p[i]</code> means <code>*(p +
140i)</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
144effect, to increment <code>i</code>.
145</p>
146<p>The &lsquo;<samp>--</samp>&rsquo; operator also works on pointers; it can be used
147to scan backwards through an array, like this:
148</p>
149<div class="example">
150<pre class="example">int
151after_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
167array specified by its arguments; that is, the index of the
168first zero of the run of zeros at the end.
169</p>
170<hr>
171<div class="header">
172<p>
173Next: <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> &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>
174</div>
175
176
177
178</body>
179</html>
Note: See TracBrowser for help on using the repository browser.