指针上的关系运算符 [英] relational operators on pointers

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

问题描述



这个主题最近出现在comp.compilers中,不过我相信

之前我在这里问过。


如果您使用关键运算符,而不是==和!=,在

指向不同对象的指针上,结果是否需要



#include< stdio.h>

int main(){

char * one =" one";

char *两个=两个;


if(一个< two || one> two)printf(not equal\\\
);

else printf(equal\ n);

}

允许打印相等吗?


这个问题适用于JVM等系统,它将对象作为opaque考虑对象,并且必须单独保留偏移量。对于

以外的运算符==和!=我只会比较偏移部分。


JVM只对对象引用进行相等测试。


- glen


The subject recently came up in comp.compilers, though I believe that
I asked here before.

If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?
#include <stdio.h>
int main() {
char *one="one";
char *two="two";

if(one<two || one>two) printf("not equal\n");
else printf("equal\n");
}
Is it allowed to print equal?

This question comes up for systems such as JVM that consider object
references as opaque, and the offset must be kept separately. For
operators other than == and != I would only compare the offset part.

JVM has only equality test for object references.

-- glen

推荐答案

文章< z28Bb.341376
In article <z28Bb.341376

275.1122431@attbi_s53> ;,

glen herrmannsfeldt< ga*@ugcs.caltech.edu>写道:
275.1122431@attbi_s53>,
glen herrmannsfeldt <ga*@ugcs.caltech.edu> wrote:
这个主题最近出现在comp.compilers中,虽然我相信我之前问过这个问题。

如果你使用关系运算符,除了==和!=,指向不同对象的指针,结果是否有任何要求?

#include< stdio.h>
int main(){
char * one =" one" ;;
char * two =" two";

if(one< two || one> two )printf(not equal\\\
);
else printf(equal\ n);
}

是否允许打印相等?<这个问题出现在诸如JVM之类的系统中,它将对象引用视为不透明,并且必须单独保留偏移量。对于除==和!=以外的
运算符,我只会比较偏移部分。
The subject recently came up in comp.compilers, though I believe that
I asked here before.

If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?
#include <stdio.h>
int main() {
char *one="one";
char *two="two";

if(one<two || one>two) printf("not equal\n");
else printf("equal\n");
}
Is it allowed to print equal?

This question comes up for systems such as JVM that consider object
references as opaque, and the offset must be kept separately. For
operators other than == and != I would only compare the offset part.




它会调用未定义的行为,因此它可以打印任何内容。或者你的

计算机可能会爆炸。


比较指针与< ;,< =,>或者> =只有当两者都指向同一个对象时才允许




It invokes undefined behavior, so it can print anything. Or your
computer might explode.

Comparing pointers with <, <=, > or >= is only allowed if both point
into the same object.





glen herrmannsfeldt写道:


glen herrmannsfeldt wrote:

这个主题最近出现在comp.compilers中,虽然我相信
我之前在这里问过。

如果你使用关于不同对象的指针之外的关系运算符,除了==和!=之外,对结果有什么要求吗?

#include< stdio.h>
int main(){
char * one =" one" ;;
char * two =" two";

if(one< two || one> ; 2)printf(不等于\ n);
其他printf(equal\ n);
}

允许打印相等?

这个问题出现在诸如JVM之类的系统中,它将对象引用视为不透明,并且偏移必须单独保存。对于==和!=以外的
运算符,我只会比较偏移部分。

JVM只对对象引用进行相等测试。

- glen

The subject recently came up in comp.compilers, though I believe that
I asked here before.

If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?

#include <stdio.h>
int main() {
char *one="one";
char *two="two";

if(one<two || one>two) printf("not equal\n");
else printf("equal\n");
}

Is it allowed to print equal?

This question comes up for systems such as JVM that consider object
references as opaque, and the offset must be kept separately. For
operators other than == and != I would only compare the offset part.

JVM has only equality test for object references.

-- glen




使用除==

和!=之外的关系运算符几乎没有任何意义。除非指针指向某些原语。否则,你是比较结构或数组的地址,这对你来说似乎是随机的,因为你无法控制编译器放置的位置

他们在创作时。


在上面的例子中,唯一的保证是它们不相等,

因为两个指针指向不同记忆中的位置。

-

Fred L. Kleinschmidt

波音助理技术研究员

技术架构师,普通用户界面服务

M / S 2R-94(206)544-5225



There is rarely any point in using relational operators other than "=="
and "!=" unless the pointers point to some primitive. Otherwise, you are
comparing addresses of structs or arrays, which would appear to you as
rather random, since you have no control over where the compiler places
them on creation.

In your example above, the only guarantee is that they are not equal,
since the two pointers point to different locations in memory.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225


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

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