From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/types%2Fptrdiff_t.html | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 devdocs/c/types%2Fptrdiff_t.html (limited to 'devdocs/c/types%2Fptrdiff_t.html') 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 @@ +

ptrdiff_t

Defined in header <stddef.h>
typedef /*implementation-defined*/ ptrdiff_t;
+

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

+ + + + +

The bit width of ptrdiff_t is not less than 17.

+
+(since C99)
(until C23) +

The bit width of ptrdiff_t is not less than 16.

+
(since C23)

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 equal to or 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).

+

Example

#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+ 
+int main(void)
+{
+    const size_t N = 100;
+    int numbers[N];
+ 
+    printf("PTRDIFF_MAX = %ld\n", PTRDIFF_MAX);
+    int *p1 = &numbers[18], *p2 = &numbers[23];
+    ptrdiff_t diff = p2 - p1;
+    printf("p2-p1 = %td\n", diff);
+}

Possible output:

+
PTRDIFF_MAX = 9223372036854775807
+p2-p1 = 5

References

See also

+ + +
unsigned integer type returned by the sizeof operator
(typedef)
byte offset from the beginning of a struct type to specified member
(function macro)
C++ documentation for ptrdiff_t
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/types/ptrdiff_t +

+
-- cgit v1.2.3