| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4177 人关注过本帖, 4 人收藏
标题:[原创] C标准中关于 指针比较 的规定
只看楼主 加入收藏
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
结帖率:91.67%
收藏(4)
已结贴  问题点数:20 回复次数:11 
[原创] C标准中关于 指针比较 的规定
观某帖有感,感叹这些基础知识竟然好多人不懂,因此特意翻译一下,有错的话,请指正

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.
如果两个指针指向同一个对象,或指向同一个数组的最后一个元素的下一个元素时,它们相等。
(举例,int arr[2];
那么 &arr[0]和&arr[0]相等、&arr[1]和&arr[1]相等、&arr[2]和&arr[2]相等,虽然arr[2]不存在,但其地址是有效的;
而 &arr[3]和&arr[3]相比较呢?属于 未定义行为。


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,
如果两个指针指向同一个聚合体(struct和array)的成员,那么声明在后的成员地址 大于 声明在前的成员地址
(举例,struct foo{ double a; int b; }; 中 &b 大于 &a。
如果不是同一个聚合体呢?属于 未定义行为。


and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values.
同一个数组中,下标大的元素的地址 大于 下标小的元素的地址
(举例,int arr[2] 中 &arr[2] > &arr[1] > &arr[0]
如果不是同一个数组呢?属于 未定义行为。


All pointers to members of the same union object compare equal.
同一个union对象中,成员地址都相同

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.
P指向某数组中的某个元素,Q指向这个数组中的最后一个元素,那么Q+1大于P。
(之前已经举例过了,对于 int arr[2],则 &arr[0]、&arr[1] 都小于 &arr[2])

In all other cases, the behavior is undefined.
其它的所有情况,其行为都是未定义的。

-------------------------------------------------------------------

对于C++而言,还规定在同一个class/struct中的访问控制权限(public,protected,private)必须一样。

-------------------------------------------------------------------

指针相减的类型是 ptrdiff_t,输出格式是 %t,举例
#include <stdio.h>

int main( void )
{
    int arr[2];
    ptrdiff_t d = &arr[2] - &arr[0];
    printf( "%td\n", d );
}



[此贴子已经被作者于2016-8-9 19:42编辑过]

2016-08-08 11:08
wanglianyi1
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:647
专家分:2067
注 册:2015-6-18
收藏
得分:10 
学习了
2016-08-08 15:36
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:10 
学习指针的新人看了认为有用,最好回复下。至少让后来者更容易看到,不至于沉底。
2016-08-09 17:08
哇哈哈公子羽
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-8-6
收藏
得分:0 
学习一下。
2016-08-13 13:37
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
收藏
得分:0 
学到新知识了

早知做人那么辛苦!  当初不应该下凡
2016-09-22 20:12
唔名剑客
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-11-23
收藏
得分:0 
很不错的
2016-11-23 23:09
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
现在才看到~怪我进论坛太迟了,早点看走少点弯路~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2016-11-23 23:24
EdmundDantes
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:62
专家分:173
注 册:2016-11-9
收藏
得分:0 
有益
2016-11-24 11:50
止步于未来
Rank: 2
等 级:论坛游民
威 望:1
帖 子:22
专家分:67
注 册:2016-11-24
收藏
得分:0 
需要时很有用的

一步一脚的向前
2016-11-24 17:01
神传奇
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-11-21
收藏
得分:0 
终于看懂了。。。
2016-11-26 15:15
快速回复:[原创] C标准中关于 指针比较 的规定
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015524 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved