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 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 | <!--
|
---|
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-Arithmetic"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <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> [<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.
|
---|
68 | It assumes that the pointer points to an element in an array, and
|
---|
69 | advances or retracts the pointer across as many array elements as the
|
---|
70 | integer specifies. Here is an example, in which adding a positive
|
---|
71 | integer advances the pointer to a later element in the same array.
|
---|
72 | </p>
|
---|
73 | <div class="example">
|
---|
74 | <pre class="example">void
|
---|
75 | incrementing_pointers ()
|
---|
76 | {
|
---|
77 | int array[5] = { 45, 29, 104, -3, 123456 };
|
---|
78 | int elt0, elt1, elt4;
|
---|
79 |
|
---|
80 | int *p = &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 ("elt0 %d elt1 %d elt4 %d.\n",
|
---|
93 | elt0, elt1, elt4);
|
---|
94 | /* <span class="roman">Prints elt0 45 elt1 29 elt4 123456.</span> */
|
---|
95 | }
|
---|
96 | </pre></div>
|
---|
97 |
|
---|
98 | <p>Here’s an example where adding a negative integer retracts the pointer
|
---|
99 | to an earlier element in the same array.
|
---|
100 | </p>
|
---|
101 | <div class="example">
|
---|
102 | <pre class="example">void
|
---|
103 | decrementing_pointers ()
|
---|
104 | {
|
---|
105 | int array[5] = { 45, 29, 104, -3, 123456 };
|
---|
106 | int elt0, elt3, elt4;
|
---|
107 |
|
---|
108 | int *p = &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 ("elt0 %d elt3 %d elt4 %d.\n",
|
---|
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
|
---|
127 | pointer value, it should be possible to subtract the pointer values
|
---|
128 | and recover that integer. That works too in C.
|
---|
129 | </p>
|
---|
130 | <div class="example">
|
---|
131 | <pre class="example">void
|
---|
132 | subtract_pointers ()
|
---|
133 | {
|
---|
134 | int array[5] = { 45, 29, 104, -3, 123456 };
|
---|
135 | int *p0, *p3, *p4;
|
---|
136 |
|
---|
137 | int *p = &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 ("%d, %d, %d, %d\n",
|
---|
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
|
---|
156 | add the integer (multiplied by object size) to the value of the
|
---|
157 | pointer. When the initial pointer and the result point into a single
|
---|
158 | array, the result is well-defined.
|
---|
159 | </p>
|
---|
160 | <p><strong>Warning:</strong> Only experts should do pointer arithmetic involving pointers
|
---|
161 | into 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
|
---|
165 | declare it is to use the typedef name <code>ptrdiff_t</code> defined in the
|
---|
166 | file <samp>stddef.h</samp>.
|
---|
167 | </p>
|
---|
168 | <p>This definition of pointer subtraction is consistent with
|
---|
169 | pointer-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’s size is not defined in that case.
|
---|
174 | Likewise, they are not allowed on pointers to function types.
|
---|
175 | However, these operations work in GNU C, and the “size of the target
|
---|
176 | type” is taken as 1.
|
---|
177 | </p>
|
---|
178 | <hr>
|
---|
179 | <div class="header">
|
---|
180 | <p>
|
---|
181 | Next: <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> [<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>
|
---|