gpt4 book ai didi

c - 两个大于 INT_MAX 的 int 指针的差值

转载 作者:行者123 更新时间:2023-11-30 19:51:02 25 4
gpt4 key购买 nike

两个 int* 有可能存在差异吗?指向同一数组元素的指针大于 INT_MAX

如果是这样,我如何访问这样一个数组中超出 INT_MAX 的元素?

最佳答案

Is is possible that the difference of two int* pointers on the elements of the same array is greater than INT_MAX?

是的,这是可能的。数组可以大于 INT_MAX元素。如此大的数组很少使用。

C提供ptrdiff_t作为指针减法的整数类型。

int *pointer_a;
int *pointer_b;
...
// pointer_a, pointer_b point to elements in the same array object.
ptrdiff_t diff = pointer_a - pointer_b;

// Use `%td` to print a `ptrdiff_t`.
printf("diff %td\n", diff);

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> ... C11dr §6.5.6 9

<小时/>

how could I access the elements of such an array that are beyond INT_MAX?

int之外的元素进行索引范围,使用指定的工作类型。

size_t是一些指定为足够宽以索引所有数组的无符号类型。

// some big array, maybe in global memory.
int big[10000000000];

....
size_t index = foo();
printf("big[%zu] = %d\n", index, big[index]);

关于c - 两个大于 INT_MAX 的 int 指针的差值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52345234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com