与C混淆 [英] Confusion with C
问题描述
每天我都证明我是C的初学者(即使我用C或/和C ++编程
超过5年)!前几天我发现代码使用了_REALY_奇怪的
sintax,类似这样:
// ------- code ---- -
int a [10];
int i;
for(i = 0; i< 10; i ++)
i [a] = i;
// ---结束------------
At首先我认为这是一些特定于编译器的_hack_,但我发现
被称为数组计算的东西?因此,使用[i]与
*((a)+(i))相同。有了这个,上面的代码有(只是一点点)逻辑。
我的问题是:
1)这是便携式的
2)这是C创作者想要拥有的,或者只是解析器使用这个
因为*(a + i)规则?
谢谢。
Every day I prove that i am beginer with C (even I program in C or/and C++
for more than 5 years)! Some days ago I find code that use _REALY_ strange
sintax, something like this:
// ------- code ------
int a[10];
int i;
for(i=0; i<10; i++)
i[a] = i;
// --- end ------------
At first I think that is some compiler specific _hack_, but i find
something that is called "array comutation"? So, using a[i] is same like
*((a)+(i)). With this, upper code have (just little bit) logic.
My questiona are:
1) is this portable
2) is this truely what C creators wanna to have, or just parser use this
becuase *(a+i) rule?
Thanks.
推荐答案
hari4063< za ************** @ pmf.unsa.ba>写道:
hari4063 <za**************@pmf.unsa.ba> wrote:
我的问题是:
1)这是便携式的吗?2)这是C创作者想拥有的,或者只是解析器使用这个
因为* (a + i)规则?
My questiona are:
1) is this portable
2) is this truely what C creators wanna to have, or just parser use this
becuase *(a+i) rule?
ISO / IEC 9899:1999(E),6.5.2.1数组下标
2后缀表达式后跟方括号[]中的表达式是数组对象元素的下标名称。下标运算符[]的定义
是E1 [E2]与(*((E1)+(E2)))相同。
由于转换适用于binary +运算符的规则,如果E1
是一个数组对象(相当于指向
数组对象的初始元素的指针),E2是一个整数,E1 [E2]指定
E1的第E2个元素(从零开始计算)。
Flo
-
Florian Weingarten | IRCnet:fw(fw_)| PGP:0x65C91285
http://hackvalue.de/~flo/ | ICQ:38564900 | Jabber: fw@jabber.ccc.de
ISO/IEC 9899:1999 (E), 6.5.2.1 Array subscripting
2 A postfix expression followed by an expression in square brackets [] is a
subscripted designation of an element of an array object. The definition
of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).
Because of the conversion rules that apply to the binary + operator, if E1
is an array object (equivalently, a pointer to the initial element of an
array object) and E2 is an integer, E1[E2] designates the E2-th element of
E1 (counting from zero).
Flo
--
Florian Weingarten | IRCnet: fw (fw_) | PGP: 0x65C91285
http://hackvalue.de/~flo/ | ICQ: 38564900 | Jabber: fw@jabber.ccc.de
hari4063写道:
hari4063 wrote:
每天,我证明我是C初学者(即使我用C或/和C ++编程超过5年)!
前几天我找到了使用_REALLY_奇怪语法的代码,如下所示:
// ------- code ------
int a [10];
int i;
for(i = 0; i< 10; i ++)
我[a] = i;
// ---结束----------- -
这是错误的代码。你应该写:
int a [10];
for(int i = 0; i< 10; ++ i)
a [i] = i;
起初我认为这是一些特定于编译器的_hack_,
但是我发现了一些被称为数组换向的东西?
所以,使用一个[i]与*((a)+(i))相同。
有了这个,上面的代码就有了(只是一点点)逻辑。
我的问题是,
1)这是便携式吗?,
2)这真的是C创作者想拥有的吗?
或者只是解析器使用这个因为*(a + i)规则?"
Every day, I prove that I am beginner with C
(even I program in C or/and C++ for more than 5 years)!
Some days ago I found code that uses _REALLY_ strange syntax,
something like this: // ------- code ------
int a[10];
int i;
for(i=0; i<10; i++)
i[a] = i;
// --- end ------------
This is bad code. You should write:
int a[10];
for (int i = 0; i < 10; ++i)
a[i] = i;
At first I think that is some compiler specific _hack_,
but I found something that is called "array commutation"?
So, using a[i] is same like *((a)+(i)).
With this, upper code have (just little bit) logic. My questions are,
1) "Is this portable?",
2) "Is this truely what C creators wanna to have?"
"Or just parser use this because *(a+i) rule?"
它是可移植的。
语言设计者意味着有这个灵活性。
您的代码没有显示数组换向的任何动机。
为什么你没有发布你找到的代码?
It is portable.
The language designers meant to have this flexibility.
You code doesn''t show any motivation for "array commutation".
Why didn''t you post the code that you found?
" E. Robert Tisdale <,E ************** @ jpl.nasa.gov>。在留言中写道
news:cl ********** @ nntp1.jpl.nasa.gov ...
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cl**********@nntp1.jpl.nasa.gov...
hari4063写道:
hari4063 wrote:
每天,我证明我是C初学者(即使我用C或/和C ++编程超过5年)!
前几天我找到了代码使用_REALLY_奇怪的语法,
类似这样的东西:
Every day, I prove that I am beginner with C
(even I program in C or/and C++ for more than 5 years)!
Some days ago I found code that uses _REALLY_ strange syntax,
something like this:
// ------- code ------
int a [10];
int i;
for(i = 0; i< 10; i ++)
我[a] = i;
// ---结束-------- ----
// ------- code ------
int a[10];
int i;
for(i=0; i<10; i++)
i[a] = i;
// --- end ------------
这是错误的代码。
This is bad code.
它完全有效。
你应该写:
int a [10];
for(int i = 0; i< 10; ++ i)
a [i] = i ;
It''s perfectly valid.
You should write:
int a[10];
for (int i = 0; i < 10; ++i)
a[i] = i;
这将导致与上面代码的完全相同的行为
。
-Mike
This will result in the exact same behavior as that
of the code above.
-Mike
这篇关于与C混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!