Post-增量为索引混淆
Post-Increment as Index Confusion
我宁愿通过显示代码来说明这一点。
如果这个 post 已经有了答案(因为我还没有找到),请随意将其复制为副本
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int test=3;
int *arr=(int*)malloc (5*sizeof (int));
for (int x=0; x<5; x++) *(arr+x)=x+1;
*(arr+(test++))=999;
for (int x=0; x<5; x++) printf ("%d ", *(arr+x));
return 0;
}
输出:
1 2 3 999 5
问题是,我说的是test++,意思是test会递增到4,对吧?但是为什么 999
被插入索引 [3] 而不是 [4]?
据我所知,test
应该在 999
分配给 arr
之前递增。这段代码有什么解释吗? (不一样吗?因为当我认为应该不一样的时候结果是一样的)
*(arr+(test++))=999;
*(arr+test)=999; test++;
/*
Or,
arr[test++]=999; //999 goes to [3] instead of [4]
arr[test]=999; test++; //The right syntax should be like this right?
*/
顺便说一句,预增量工作得很好,就像我做 arr[++test]=999
一样,999
将被分配给索引 [4]。
对不起,如果我说的太乱了,如果你觉得我的解释有什么不对的地方,我希望这个post,其他人也得到了糊涂可以看懂。
Pre-increment operator : A pre-increment operator( ++i
) is used to increment
the value of a variable before using it in a expression. In the
Pre-Increment, value is first incremented and then used inside the
expression.
Post-increment operator: A post-increment operator ( i++
) is used to
increment the value of variable after executing expression completely
in which post increment is used. In the Post-Increment, value is first
used in a expression and then incremented.
因此,在您的情况下,test++
将首先 return 值,即 3。然后它会递增。因此,arr[test++]
转到索引 3 而不是 4。如果您这样做 - arr[++test] = 999
,那么首先 test 的值将增加到值 4,然后这将是在语句中用作索引。这是 post-increment 和 pre-increment 之间的主要区别。
希望这能消除您的疑虑!
是什么让你感到困惑。结果是:1 2 3 999 5 使用 gcc。
我无法重现结果
1 2 3 4 999 5
运行你的程序我拿到了
1 2 3 999 5
我认为你的问题有错别字。
至于问题
The question is, there I say test++ which means test will be
incremented to 4 right? But why 999 is inserted to the index [3]
instead of [4]?
然后根据C标准(6.5.2.4后缀递增和递减运算符)
2 The result of the postfix ++ operator is the value of the operand.
As a side effect, the value of the operand object is incremented (that
is, the value 1 of the appropriate type is added to it).
所以你可以考虑这个说法
*(arr+(test++))=999
喜欢
*(arr+(test))=999
++test;
我宁愿通过显示代码来说明这一点。
如果这个 post 已经有了答案(因为我还没有找到),请随意将其复制为副本
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int test=3;
int *arr=(int*)malloc (5*sizeof (int));
for (int x=0; x<5; x++) *(arr+x)=x+1;
*(arr+(test++))=999;
for (int x=0; x<5; x++) printf ("%d ", *(arr+x));
return 0;
}
输出:
1 2 3 999 5
问题是,我说的是test++,意思是test会递增到4,对吧?但是为什么 999
被插入索引 [3] 而不是 [4]?
据我所知,test
应该在 999
分配给 arr
之前递增。这段代码有什么解释吗? (不一样吗?因为当我认为应该不一样的时候结果是一样的)
*(arr+(test++))=999;
*(arr+test)=999; test++;
/*
Or,
arr[test++]=999; //999 goes to [3] instead of [4]
arr[test]=999; test++; //The right syntax should be like this right?
*/
顺便说一句,预增量工作得很好,就像我做 arr[++test]=999
一样,999
将被分配给索引 [4]。
对不起,如果我说的太乱了,如果你觉得我的解释有什么不对的地方,我希望这个post,其他人也得到了糊涂可以看懂。
Pre-increment operator : A pre-increment operator(
++i
) is used to increment the value of a variable before using it in a expression. In the Pre-Increment, value is first incremented and then used inside the expression.Post-increment operator: A post-increment operator (
i++
) is used to increment the value of variable after executing expression completely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented.
因此,在您的情况下,test++
将首先 return 值,即 3。然后它会递增。因此,arr[test++]
转到索引 3 而不是 4。如果您这样做 - arr[++test] = 999
,那么首先 test 的值将增加到值 4,然后这将是在语句中用作索引。这是 post-increment 和 pre-increment 之间的主要区别。
希望这能消除您的疑虑!
是什么让你感到困惑。结果是:1 2 3 999 5 使用 gcc。
我无法重现结果
1 2 3 4 999 5
运行你的程序我拿到了
1 2 3 999 5
我认为你的问题有错别字。
至于问题
The question is, there I say test++ which means test will be incremented to 4 right? But why 999 is inserted to the index [3] instead of [4]?
然后根据C标准(6.5.2.4后缀递增和递减运算符)
2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).
所以你可以考虑这个说法
*(arr+(test++))=999
喜欢
*(arr+(test))=999
++test;