指针相等 [英] pointer equality

查看:85
本文介绍了指针相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在K& R" C ++编程语言(第2版ANSI C版)中,参考

手册指出(第7.9和7.10段)指针比较是

对于没有指向同一个对象的指针,未定义。


所以如果我们有


const char * foo =" foo" ,* bar =" bar" ;

int foobar =(foo == bar);


是否意味着foobar未定义?


我知道关系运算符(<,< =,> =,>)不能用于指针

到不同的对象,但是等于运算符==,!=?


问候,

Ike


邮寄到ike at ibe dot nl

In K&R "The C++ programming language (2nd ANSI C edition), the reference
manual states (paragraphs 7.9 and 7.10) that pointer comparison is
undefined for pointers that do not point to the same object.

So if we have

const char * foo = "foo" , * bar = "bar" ;
int foobar = ( foo == bar ) ;

would it mean that foobar is undefined?

I knew that relational operators (<,<=,>=,>) cannot be used on pointers
to different objects, but how about equality operators ==,!= ?

Regards,
Ike

mail to ike at iae dot nl

推荐答案

2004年4月6日星期二21:25:13 GMT,Ike Naar< no **** @ nospam.invalid>写道:
On Tue, 06 Apr 2004 21:25:13 GMT, Ike Naar <no****@nospam.invalid> wrote:
在K& R" C ++编程语言(第2版ANSI C版)中,参考指南比较为手册指出(第7.9和7.10段)对于没有指向同一个对象的指针,未定义。

如果我们有

const char * foo =" foo" ,* bar =" bar" ;
int foobar =(foo == bar);

这是否意味着foobar未定义?


你怎么看?我正在看:


指向相同类型的物体的指针(忽略任何限定符)

可以比较
我知道关系运算符(<,< =,> =,>)不能用于指针
到不同的对象


为什么不呢?

int a [10];

int * p1 =& a [3];

int * p2 =& a [4];

if(p2> p1)

...


完全合法。

,但怎么样平等运算符==,!=?


当然,要测试指针是否指向同一个对象......

-leor

问候,
Ike
给ike发送邮件给点nl
In K&R "The C++ programming language (2nd ANSI C edition), the reference
manual states (paragraphs 7.9 and 7.10) that pointer comparison is
undefined for pointers that do not point to the same object.

So if we have

const char * foo = "foo" , * bar = "bar" ;
int foobar = ( foo == bar ) ;

would it mean that foobar is undefined?
How are you reading that? I''m seeing:

"Pointers to objects of the same type (ignoring any qualifiers)
may be compared"
I knew that relational operators (<,<=,>=,>) cannot be used on pointers
to different objects
Why not?
int a[10];
int *p1 = &a[3];
int *p2 = &a[4];
if (p2 > p1)
...

Perfectly legal.
, but how about equality operators ==,!= ?
Of course, to test if the pointers point to the same object or not...
-leor

Regards,
Ike

mail to ike at iae dot nl




-

Leor Zolman - - BD软件--- www.bdsoft.com

开 - C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


2004年4月6日星期二21:25:13 GMT,Ike Naar< no **** @ nospam.invalid>写道:
On Tue, 06 Apr 2004 21:25:13 GMT, Ike Naar <no****@nospam.invalid> wrote:
在K& R" C ++编程语言(第2版ANSI C版)中,参考指南比较为手册指出(第7.9和7.10段)对于没有指向同一个对象的指针,未定义。

如果我们有

const char * foo =" foo" ,* bar =" bar" ;
int foobar =(foo == bar);

这是否意味着foobar未定义?


你怎么看?我正在看:


指向相同类型的物体的指针(忽略任何限定符)

可以比较
我知道关系运算符(<,< =,> =,>)不能用于指针
到不同的对象


为什么不呢?

int a [10];

int * p1 =& a [3];

int * p2 =& a [4];

if(p2> p1)

...


完全合法。

,但怎么样平等运算符==,!=?


当然,要测试指针是否指向同一个对象......

-leor

问候,
Ike
给ike发送邮件给点nl
In K&R "The C++ programming language (2nd ANSI C edition), the reference
manual states (paragraphs 7.9 and 7.10) that pointer comparison is
undefined for pointers that do not point to the same object.

So if we have

const char * foo = "foo" , * bar = "bar" ;
int foobar = ( foo == bar ) ;

would it mean that foobar is undefined?
How are you reading that? I''m seeing:

"Pointers to objects of the same type (ignoring any qualifiers)
may be compared"
I knew that relational operators (<,<=,>=,>) cannot be used on pointers
to different objects
Why not?
int a[10];
int *p1 = &a[3];
int *p2 = &a[4];
if (p2 > p1)
...

Perfectly legal.
, but how about equality operators ==,!= ?
Of course, to test if the pointers point to the same object or not...
-leor

Regards,
Ike

mail to ike at iae dot nl




-

Leor Zolman - - BD软件--- www.bdsoft.com

开 - C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


在文章< Ze **************** @ typhoon.bart.nl>,

Ike Naar< no **** @ nospam.invalid>写道:
In article <Ze****************@typhoon.bart.nl>,
Ike Naar <no****@nospam.invalid> wrote:
在K& R" C ++编程语言(第2版ANSI C版)中,参考指南比较为手册指出(第7.9和7.10段)对于没有指向同一个对象的指针,未定义。

如果我们有

const char * foo =" foo" ,* bar =" bar" ;
int foobar =(foo == bar);

这是否意味着foobar未定义?
我知道关系运算符(<,< =,> =,>)不能用于指向不同对象的指针,但是等于运算符怎么样==,!=?
In K&R "The C++ programming language (2nd ANSI C edition), the reference
manual states (paragraphs 7.9 and 7.10) that pointer comparison is
undefined for pointers that do not point to the same object.

So if we have

const char * foo = "foo" , * bar = "bar" ;
int foobar = ( foo == bar ) ;

would it mean that foobar is undefined? I knew that relational operators (<,<=,>=,>) cannot be used on pointers
to different objects, but how about equality operators ==,!= ?




< < => > =不能合法地用于指向不同对象的指针; ==

和!=可以。所以你提到的那本书是错的,或者当你读到它时你错过了一些东西。你可能想重新阅读它,看看它是否是b $ b提及关系运营商。和平等操作者。如果你是

确定你不会误解任何东西,你应该从这本书中更多地发布一些




< <= > >= cannot be legally used on pointers to different objects; ==
and != can. So either the book you mentioned is wrong, or you missed
something when you read it. You might want to re-read it and see if it
mentions "relational operators" and "equality operators". If you are
sure you don''t misunderstand anything, you should probably post a bit
more literally from that book.


这篇关于指针相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆