On this page
ptrdiff_t
Prev: size_t | Up: stddef.h | Next: NULL
Rel: stddef.h, stdint, stdbool & limits Guide
Ref: cppreference — ptrdiff_t
Tags: #public #prog #c #stdlib #stddef #tier/standard
Include
#include <stddef.h>
Signature & Overview
typedef /* signed */ ptrdiff_t;
Signed integer type produced by subtracting two pointers into the same array (p2 - p1).
Notes & Pitfalls
- Signed result of subtracting two pointers into the SAME array: Print with
%td; subtracting pointers to unrelated objects is UB.
Example
#include <assert.h>
#include <stddef.h>
int main(void) {
int arr[5] = {0};
ptrdiff_t dist = &arr[4] - &arr[1];
assert(dist == 3);
}
Linked from
Related notes
- tuts/c/stdlib/stddef/NULL Linked here; links to this page; shared tags: #c, #prog, #stddef, #stdlib, #tier
- tuts/c/stdlib/stddef/size_t Linked here; links to this page; shared tags: #c, #prog, #stddef, #stdlib, #tier
- tuts/c/stdlib/stddef Linked here; links to this page; shared tags: #c, #prog, #stddef, #stdlib
- tuts/c/stdlib/stdint_stdbool_and_limits Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stddef/max_align_t shared tags: #c, #prog, #stddef, #stdlib, #tier
- tuts/c/stdlib/stddef/offsetof shared tags: #c, #prog, #stddef, #stdlib, #tier
- tuts/c/stdlib/assert/assert shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/assert/static_assert shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/ctype/isalpha shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/ctype/isdigit shared tags: #c, #prog, #stdlib, #tier