summaryrefslogtreecommitdiff
path: root/devdocs/c/types%2Fptrdiff_t.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/types%2Fptrdiff_t.html
new repository
Diffstat (limited to 'devdocs/c/types%2Fptrdiff_t.html')
-rw-r--r--devdocs/c/types%2Fptrdiff_t.html56
1 files changed, 56 insertions, 0 deletions
diff --git a/devdocs/c/types%2Fptrdiff_t.html b/devdocs/c/types%2Fptrdiff_t.html
new file mode 100644
index 00000000..a576a926
--- /dev/null
+++ b/devdocs/c/types%2Fptrdiff_t.html
@@ -0,0 +1,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>