运算符的交换性[] [英] Commutativity of operator []

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

问题描述

想知道,在无望的夜晚想知道......


int i;

int sum = 0;

int array [5] = {1,2,3,4,5};


for(i = 0; i< 5; i ++)sum + = array [i];

for(i = 0; i< 5; i ++)sum + = i [array];


我理解为什么operator []是可交换的,但我'我很好奇

是否有人发现了索引[地址]

符号的实际用途。


FAQ说:在混淆的C竞赛之外没有有用的应用程序。


你说什么?


谢谢!

Wondering, wondering in hopeless night...

int i;
int sum=0;
int array[5] = {1,2,3,4,5};

for (i=0; i<5; i++) sum += array[i];
for (i=0; i<5; i++) sum += i[array];

I understand why operator [] is commutative, but I''m curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?

Thanks!

推荐答案

lw * ********@hotmail.com 写道:
想知道,想在绝望的夜晚......

int i;
int sum = 0;
int array [5] = {1,2,3,4,5};

for(i = 0; i< 5; i ++)sum + = array [i];
for(i = 0; i< 5; i ++)sum + = i [array];

我是unde rstand为什么operator []是可交换的,但我很好奇
是否有人发现了索引[地址]
符号的实际用途。

你说什么?
Wondering, wondering in hopeless night...

int i;
int sum=0;
int array[5] = {1,2,3,4,5};

for (i=0; i<5; i++) sum += array[i];
for (i=0; i<5; i++) sum += i[array];

I understand why operator [] is commutative, but I''m curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?




自从这两个形式是相同的,我[数组]总是可以写成

array [i](学生免责声明:假设数组)和i不是阴险的

宏),唯一的区别就是可读性,我认为没有

是任何情况下我的[数组]会在理智的编程中首选。


Robert Gamble



Since the two forms are identical and i[array] can always be written as
array[i] (pedant disclaimer: assuming "array" and "i" are not sinister
macros), the only difference would be readability, I do not think there
are any cases where i[array] would be preferred in sane programming.

Robert Gamble


发布:

posted:

我理解为什么operator []是可交换的,但我很好奇
是否有人发现了index [address]
表示法的实际用途。
I understand why operator [] is commutative, but I''m curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.



你说的就好像是索引[地址]一样。符号是为特定原因制定的......


我似乎更像是副产品 C语言如何处理方形

括号运算符。给出以下表达式:


a [b]


,它被解释为:

* (a + b)


因此,您可以看到订单无关紧要 - 您可以轻松写完




*(b + a)

如果开始使用负面的

索引,可能事情变得有点可疑......不确定。


You word that as if the "index[address]" notation was formulated for a
specific reason...

I seem it more as a "by-product" of how the C language handles the square
brackets operator. Given the following expression:

a[b]

, it''s interpreted EXACTLY as:
*(a + b)

Therefore, you can see how the order doesn''t matter -- you could as
easily have written:

*(b + a)
Maybe things get a little fishy though if you start using negative
indexes... not sure.


lw ********* @ hotmail .com 说:
想知道,想知道在绝望的夜晚......

int i;
int sum = 0;
int array [5] = {1,2,3,4,5};

for(i = 0; i< 5; i ++)sum + = array [i];
for(i = 0; i< 5; i ++)sum + = i [array];

我理解为什么operator []是可交换的,但我很好奇
常见问题解答说:没有任何有用的应用程序在混淆的C竞赛。

你说什么?
Wondering, wondering in hopeless night...

int i;
int sum=0;
int array[5] = {1,2,3,4,5};

for (i=0; i<5; i++) sum += array[i];
for (i=0; i<5; i++) sum += i[array];

I understand why operator [] is commutative, but I''m curious as to
whether anyone has ever found a practical use for the "index[address]"
notation.

FAQ says: "no useful application outside of the Obfuscated C Contest."

What say you?




这是几年前在comp.lang.c上提出的。某个人或其他人(我忘了

谁,我很害怕)指出如果你有一堆包含

相关数据的数组,例如char * Name [],int Length [],int Pitch [],int Diameter [],

,比如说int product,那么你可能想写:


DisplayS(hnd,x,y,Product [Name]);

DisplayI(hnd,x,y + 1,Product [Length]);

DisplayI(hnd,x,y + 2,Product [Pitch]);

DisplayI(hnd,x,y + 2,Product [Diameter]);


虽然为什么你不会在这种情况下使用结构数组,但是坦率地说,超过我。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



This was raised on comp.lang.c a few years ago. Someone or other (I forget
who, I''m afraid) pointed out that if you have a bunch of arrays containing
related data, e.g. char *Name[], int Length[], int Pitch[], int Diameter[],
and, say, int Product, then you might want to write:

DisplayS(hnd, x, y, Product[Name]);
DisplayI(hnd, x, y + 1, Product[Length]);
DisplayI(hnd, x, y + 2, Product[Pitch]);
DisplayI(hnd, x, y + 2, Product[Diameter]);

although why you wouldn''t use a struct array in that circumstance is, quite
frankly, beyond me.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


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

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