Namespaces
Variants
Views
Actions

ptrdiff_t

From cppreference.com
< c‎ | types
Revision as of 13:22, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <stddef.h>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td class="t-dcl-nopad">
typedef /*implementation-defined*/ ptrdiff_t;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

ptrdiff_t is the signed integer type of the result of subtracting two pointers.

Notes

ptrdiff_t is used for pointer arithmetic and array indexing, if negative values are possible. Programs that use other types, such as int, may fail on, e.g. 64-bit systems when the index exceeds INT_MAX or if it relies on 32-bit modular arithmetic.

Only pointers to elements of the same array (including the pointer one past the end of the array) may be subtracted from each other.

If an array is so large (greater than PTRDIFF_MAX elements, but less than SIZE_MAX bytes), that the difference between two pointers may not be representable as ptrdiff_t, the result of subtracting two such pointers is undefined.

For char arrays shorter than PTRDIFF_MAX, ptrdiff_t acts as the signed counterpart of size_t: it can store the size of the array of any type and is, on most platforms, synonymous with intptr_t).

See also

Template:c/types/dcl list size tTemplate:c/types/dcl list offsetof
C++ documentation for ptrdiff_t