summaryrefslogtreecommitdiff
path: root/devdocs/c/types%2Fptrdiff_t.html
blob: a576a9261b6cefe7ba2c0f2f84a35675ce72dbd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    <h1 id="firstHeading" class="firstHeading">ptrdiff_t</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stddef.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">typedef /*implementation-defined*/ ptrdiff_t;</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p><code>ptrdiff_t</code> is the signed integer type of the result of <a href="../language/operator_arithmetic#Pointer_arithmetic" title="c/language/operator arithmetic">subtracting two pointers</a>.</p>
<table class="t-rev-begin"> <tr class="t-rev t-since-c99 t-until-c23">
<td> <p>The bit width of <code>ptrdiff_t</code> is not less than 17.</p>
</td> <td>
<span class="t-mark-rev t-since-c99">(since C99)</span><br><span class="t-mark-rev t-until-c23">(until C23)</span>
</td>
</tr> <tr class="t-rev t-since-c23">
<td> <p>The bit width of <code>ptrdiff_t</code> is not less than 16.</p>
</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
</tr> </table>  <h3 id="Notes"> Notes</h3> <p><code>ptrdiff_t</code> is used for pointer arithmetic and array indexing, if negative values are possible. Programs that use other types, such as <code>int</code>, may fail on, e.g. 64-bit systems when the index exceeds <code><a href="limits" title="c/types/limits">INT_MAX</a></code> or if it relies on 32-bit modular arithmetic.</p>
<p>Only pointers to elements of the same array (including the pointer one past the end of the array) may be subtracted from each other.</p>
<p>If an array is so large (greater than <code><a href="limits" title="c/types/limits">PTRDIFF_MAX</a></code> elements, but equal to or less than <code><a href="limits" title="c/types/limits">SIZE_MAX</a></code> bytes), that the difference between two pointers may not be representable as <code>ptrdiff_t</code>, the result of subtracting two such pointers is undefined.</p>
<p>For char arrays shorter than <code><a href="limits" title="c/types/limits">PTRDIFF_MAX</a></code>, <code>ptrdiff_t</code> acts as the signed counterpart of <code><a href="size_t" title="c/types/size t">size_t</a></code>: it can store the size of the array of any type and is, on most platforms, synonymous with <code>intptr_t</code>).</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stddef.h&gt;
#include &lt;stdint.h&gt;
#include &lt;stdio.h&gt;
 
int main(void)
{
    const size_t N = 100;
    int numbers[N];
 
    printf("PTRDIFF_MAX = %ld\n", PTRDIFF_MAX);
    int *p1 = &amp;numbers[18], *p2 = &amp;numbers[23];
    ptrdiff_t diff = p2 - p1;
    printf("p2-p1 = %td\n", diff);
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">PTRDIFF_MAX = 9223372036854775807
p2-p1 = 5</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.19 Common definitions &lt;stddef.h&gt; (p: 211) </li>
<li> 7.20.3 Limits of other integer types (p: 215) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.19 Common definitions &lt;stddef.h&gt; (p: 288) </li>
<li> 7.20.3 Limits of other integer types (p: 293) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.17 Common definitions &lt;stddef.h&gt; (p: 253) </li>
<li> 7.18.3 Limits of other integer types (p: 258) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.1.6 Common definitions &lt;stddef.h&gt; </li></ul>
</ul>                  <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="size_t" title="c/types/size t"> <span class="t-lines"><span>size_t</span></span></a></div> </td> <td> unsigned integer type returned by the <a href="../language/sizeof" title="c/language/sizeof"><code>sizeof</code></a> operator <br> <span class="t-mark">(typedef)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="offsetof" title="c/types/offsetof"> <span class="t-lines"><span>offsetof</span></span></a></div> </td> <td> byte offset from the beginning of a struct type to specified member <br> <span class="t-mark">(function macro)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/types/ptrdiff_t" title="cpp/types/ptrdiff t">C++ documentation</a></span> for <code>ptrdiff_t</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/types/ptrdiff_t" class="_attribution-link">https://en.cppreference.com/w/c/types/ptrdiff_t</a>
  </p>
</div>