source: public/doc/gnu-c/Pointer-Arithmetic.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.2 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 Arithmetic (GNU C Language Manual)</title>
23
24<meta name="description" content="Pointer Arithmetic (GNU C Language Manual)">
25<meta name="keywords" content="Pointer Arithmetic (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="Pointers-and-Arrays.html" rel="next" title="Pointers and Arrays">
34<link href="Pointer-Comparison.html" rel="prev" title="Pointer Comparison">
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-Arithmetic"></span><div class="header">
59<p>
60Next: <a href="Pointers-and-Arrays.html" accesskey="n" rel="next">Pointers and Arrays</a>, Previous: <a href="Pointer-Comparison.html" accesskey="p" rel="prev">Pointer Comparison</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-Arithmetic-1"></span><h3 class="section">14.10 Pointer Arithmetic</h3>
64<span id="index-pointer-arithmetic"></span>
65<span id="index-arithmetic_002c-pointer"></span>
66
67<p>Adding an integer (positive or negative) to a pointer is valid in C.
68It assumes that the pointer points to an element in an array, and
69advances or retracts the pointer across as many array elements as the
70integer specifies. Here is an example, in which adding a positive
71integer advances the pointer to a later element in the same array.
72</p>
73<div class="example">
74<pre class="example">void
75incrementing_pointers ()
76{
77 int array[5] = { 45, 29, 104, -3, 123456 };
78 int elt0, elt1, elt4;
79
80 int *p = &amp;array[0];
81 /* <span class="roman">Now <code>p</code> points at element 0. Fetch it.</span> */
82 elt0 = *p;
83
84 ++p;
85 /* <span class="roman">Now <code>p</code> points at element 1. Fetch it.</span> */
86 elt1 = *p;
87
88 p += 3;
89 /* <span class="roman">Now <code>p</code> points at element 4 (the last). Fetch it.</span> */
90 elt4 = *p;
91
92 printf (&quot;elt0 %d elt1 %d elt4 %d.\n&quot;,
93 elt0, elt1, elt4);
94 /* <span class="roman">Prints elt0 45 elt1 29 elt4 123456.</span> */
95}
96</pre></div>
97
98<p>Here&rsquo;s an example where adding a negative integer retracts the pointer
99to an earlier element in the same array.
100</p>
101<div class="example">
102<pre class="example">void
103decrementing_pointers ()
104{
105 int array[5] = { 45, 29, 104, -3, 123456 };
106 int elt0, elt3, elt4;
107
108 int *p = &amp;array[4];
109 /* <span class="roman">Now <code>p</code> points at element 4 (the last). Fetch it.</span> */
110 elt4 = *p;
111
112 --p;
113 /* <span class="roman">Now <code>p</code> points at element 3. Fetch it.</span> */
114 elt3 = *p;
115
116 p -= 3;
117 /* <span class="roman">Now <code>p</code> points at element 0. Fetch it.</span> */
118 elt0 = *p;
119
120 printf (&quot;elt0 %d elt3 %d elt4 %d.\n&quot;,
121 elt0, elt3, elt4);
122 /* <span class="roman">Prints elt0 45 elt3 -3 elt4 123456.</span> */
123}
124</pre></div>
125
126<p>If one pointer value was made by adding an integer to another
127pointer value, it should be possible to subtract the pointer values
128and recover that integer. That works too in C.
129</p>
130<div class="example">
131<pre class="example">void
132subtract_pointers ()
133{
134 int array[5] = { 45, 29, 104, -3, 123456 };
135 int *p0, *p3, *p4;
136
137 int *p = &amp;array[4];
138 /* <span class="roman">Now <code>p</code> points at element 4 (the last). Save the value.</span> */
139 p4 = p;
140
141 --p;
142 /* <span class="roman">Now <code>p</code> points at element 3. Save the value.</span> */
143 p3 = p;
144
145 p -= 3;
146 /* <span class="roman">Now <code>p</code> points at element 0. Save the value.</span> */
147 p0 = p;
148
149 printf (&quot;%d, %d, %d, %d\n&quot;,
150 p4 - p0, p0 - p0, p3 - p0, p0 - p3);
151 /* <span class="roman">Prints 4, 0, 3, -3.</span> */
152}
153</pre></div>
154
155<p>The addition operation does not know where arrays are. All it does is
156add the integer (multiplied by object size) to the value of the
157pointer. When the initial pointer and the result point into a single
158array, the result is well-defined.
159</p>
160<p><strong>Warning:</strong> Only experts should do pointer arithmetic involving pointers
161into different memory objects.
162</p>
163<p>The difference between two pointers has type <code>int</code>, or
164<code>long</code> if necessary (see <a href="Integer-Types.html">Integer Types</a>). The clean way to
165declare it is to use the typedef name <code>ptrdiff_t</code> defined in the
166file <samp>stddef.h</samp>.
167</p>
168<p>This definition of pointer subtraction is consistent with
169pointer-integer addition, in that <code>(p3 - p1) + p1</code> equals
170<code>p3</code>, as in ordinary algebra.
171</p>
172<p>In standard C, addition and subtraction are not allowed on <code>void
173*</code>, since the target type&rsquo;s size is not defined in that case.
174Likewise, they are not allowed on pointers to function types.
175However, these operations work in GNU C, and the &ldquo;size of the target
176type&rdquo; is taken as 1.
177</p>
178<hr>
179<div class="header">
180<p>
181Next: <a href="Pointers-and-Arrays.html" accesskey="n" rel="next">Pointers and Arrays</a>, Previous: <a href="Pointer-Comparison.html" accesskey="p" rel="prev">Pointer Comparison</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>
182</div>
183
184
185
186</body>
187</html>
Note: See TracBrowser for help on using the repository browser.