gpt4 book ai didi

c - ISO C11 标准在哪里声明比较两个不指向同一数组的指针是未定义的行为?

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:46 26 4
gpt4 key购买 nike

ISO C11 标准在哪里声明比较两个不指向同一数组的指针(使用 <><=>=)是未定义的行为?

最佳答案

嗯,6.5.8p5 from C11 draft很清楚:

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.

例如,“其他情况”是两个指针指向不同数组的情况。

请注意,有关 pointer provenance 的工作仍在进行中希望 future 的标准能够清除边缘情况。

关于c - ISO C11 标准在哪里声明比较两个不指向同一数组的指针是未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57733526/

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