ptr3 = (int *) (ptr2 – ptr1)
ptr2 - ptr1
gives the difference between the subscripts. Casting ptr2 - ptr1
to an integer confuses me since it is already an integer. Why do that? EDIT:- ptr1, ptr2,ptr3 are all pointers
Pointer subtraction yields the result of type ptrdiff_t
.
Quoting C11
, chapter §6.5.6/P9
When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements. The size of the result is implementation-defined, and its type (a signed integer type) is
ptrdiff_t
defined in the<stddef.h>
header [...]
Here, the attempt is to cast that to an int *
, which does not appear to be proper.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.