数组索引 [英] Array indexing

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

问题描述

以下代码使用gcc 3.2.2和Visual C ++ 6编译:


#include< stdio.h>

int main()

{

int a [2] = {3,4};

printf("%d \ n",0 [a] ]);

返回0;

}


不寻常的部分是0 [a]。编译器将其转换为

a [0]没有警告。有没有其他人看到这种行为,或者更好

,有人可以解释一下吗?看起来像是index [array]是

相当于array [index]。

The following code compiles with gcc 3.2.2 and Visual C++ 6:

#include <stdio.h>
int main()
{
int a[2] = {3, 4};
printf ("%d\n", 0[a]);
return 0;
}

The unusual part is the "0[a]". The compilers translate this to
"a[0]" with no warning. Has anyone else seen this behavior, or better
yet, can anyone explain it? It seems like "index[array]" is
equivalent to "array[index]".

推荐答案

Joshua Neuheisel< jn **** ****@msn.com>潦草地写道:
Joshua Neuheisel <jn********@msn.com> scribbled the following:
以下代码用gcc 3.2.2和Visual C ++ 6编译:
#include< stdio.h>
int main()
{
int a [2] = {3,4};
printf("%d \ n",0 [a]);
返回0;
}
不寻常的部分是0 [a]。编译器将其转换为
a [0]。没有警告。有没有其他人看过这种行为,或者更好
然而,任何人都可以解释它吗?看起来像是index [array]
相当于array [index]。
The following code compiles with gcc 3.2.2 and Visual C++ 6: #include <stdio.h>
int main()
{
int a[2] = {3, 4};
printf ("%d\n", 0[a]);
return 0;
} The unusual part is the "0[a]". The compilers translate this to
"a[0]" with no warning. Has anyone else seen this behavior, or better
yet, can anyone explain it? It seems like "index[array]" is
equivalent to "array[index]".




是的,肯定是。更重要的是,任何表达,例如a [b]等。

相当于b [a]如果a,b中只有一个是指针而另一个是b / b
是标量整数。

这是C的故意特征,完全是标准的,可移植的,

和安全。这是因为a [b]被内部翻译为

*(a + b),+是可交换的。


- -

/ - Joona Palaste(pa*****@cc.helsinki.fi)---------------------- ----- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

记住:只有三种人 - 那些可以计算的人和那些可以'b
'的人't。"

- Vampyra



Yes, it certainly is. What''s more, any expression such as "a[b]" is
equivalent to "b[a]" if exactly one of a, b is a pointer and the other
is a scalar integer.
This is a deliberate feature of C, and is completely standard, portable,
and safe. It''s due to the fact that a[b] gets internally translated to
*(a+b), and + is commutative.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Remember: There are only three kinds of people - those who can count and those
who can''t."
- Vampyra


Joshua Neuheisel写道:
Joshua Neuheisel wrote:
以下代码用gcc 3.2编译。 2和Visual C ++ 6:
#include< stdio.h>
int main()
{
int a [2] = {3,4} ;
printf("%d \ nn",0 [a]);
返回0;
}

不寻常的部分是0并[a]" ;.编译器将其转换为
a [0]。没有警告。有没有其他人看过这种行为,或者更好
然而,任何人都可以解释它吗?看起来像是index [array]
相当于array [index]。
The following code compiles with gcc 3.2.2 and Visual C++ 6:

#include <stdio.h>
int main()
{
int a[2] = {3, 4};
printf ("%d\n", 0[a]);
return 0;
}

The unusual part is the "0[a]". The compilers translate this to
"a[0]" with no warning. Has anyone else seen this behavior, or better
yet, can anyone explain it? It seems like "index[array]" is
equivalent to "array[index]".




这实际上是有意义的。


说& a [0]是0x30000000


然后(int)a == 0x30000000有效。


然后0 [a]只是意味着*(& 0 + a)是*(0 + 0x30000000)== *(0x30000000)


假设发生了什么事。不,索引[array]!= array [index]

因为索引[array]不会将索引乘以sizeof array [0]。


Tom



That actually makes sense.

Say &a[0] is 0x30000000

Then (int)a == 0x30000000 is valid.

Then 0[a] simply means *(&0 + a) which is *(0 + 0x30000000) == *(0x30000000)

Assuming that is what happened. And no, index[array] != array[index]
since index[array] will not multiply index by the sizeof array[0].

Tom


Tom St Denis写道:
Tom St Denis wrote:
假设发生了什么事。不,index [array]!= array [index]
因为index [array]不会将sizeof乘以sizeof [0]。
Assuming that is what happened. And no, index[array] != array[index]
since index[array] will not multiply index by the sizeof array[0].




哎呀,确实如此。


好​​吧。


汤姆



Oops, it does.

Oh well.

Tom


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

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