C语言 数组中的前增量与后增量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16869020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pre increment vs Post increment in array
提问by Eric Wich
I am learning programming and I have started from C language. I was reading Let us C book. And I was going through this program in that book.
我正在学习编程,我是从 C 语言开始的。我在读Let us C书。我正在阅读那本书中的这个程序。
main( )
{
int a[5] = { 5, 1, 15, 20, 25 } ;
int i, j, k = 1, m ;
i = ++a[1] ;
j = a[1]++ ;
m = a[i++] ;
printf ( "\n%d %d %d", i, j, m ) ;
}
My understanding was, it will print i as 2, j as 1and m as 15
我的理解是,它会打印i as 2,j as 1并且m as 15
But somehow it is printing as i as 3, j as 2and m as 15? Why is it so?
但不知何故它打印为 i as 3,j as 2和m as 15? 为什么会这样?
Below is my understanding-
以下是我的理解——
b = x++;
In this example suppose the value of variable ‘x' is 5 then value of variable ‘b' will be 5 because old value of ‘x' is used.
b = ++y;
In this example suppose the value of variable ‘y' is 5 then value of variable ‘b' will be 6 because the value of ‘y' gets modified before using it in a expression.
Is there anything wrong in my understanding?
我的理解有什么问题吗?
回答by Eric Wich
You hit the nail on the head. Your understanding is correct. The difference between pre and post increment expressions is just like it sounds. Pre-incrementation means the variable is incremented before the expression is set or evaluated. Post-incrementation means the expression is set or evaluated, and then the variable is altered. It's easy to think of it as a two step process.
你击中了要害。你的理解是正确的。前后增量表达式之间的区别就像听起来一样。预增量意味着变量在设置或计算表达式之前递增。后增量意味着设置或评估表达式,然后更改变量。很容易将其视为一个两步过程。
b = x++;
is really:
是真的:
b = x;
x++;
and
和
b = ++x;
is really:
是真的:
x++;
b = x;
EDIT: The tricky part of the examples you provided (which probably threw you off) is that there's a huge difference between an array index, and its value.
编辑:您提供的示例中棘手的部分(可能会让您失望)是数组索引与其值之间存在巨大差异。
i = ++a[1];
That means increment the value stored at a[1], and then set it to the variable i.
这意味着增加存储在 a[1] 中的值,然后将其设置为变量 i。
m = a[i++];
This one means set m to the value of a[i], then increment i. The difference between the two is a pretty big distinction and can get confusing at first.
这意味着将 m 设置为 a[i] 的值,然后增加 i。两者之间的区别非常大,一开始可能会让人感到困惑。
Second EDIT: breakdown of the code
第二次编辑:代码分解
{
int a[5] = { 5, 1, 15, 20, 25 } ;
int i, j, k = 1, m ;
i = ++a[1] ;
j = a[1]++ ;
m = a[i++] ;
printf ( "\n%d %d %d", i, j, m ) ;
}
First:
第一的:
i = ++a[1];
At this point we know a[1] = 1 (remember arrays are zero indexed). But we increment it first. Therefore i = 2.
此时我们知道 a[1] = 1(记住数组是零索引的)。但是我们先增加它。因此 i = 2。
j = a[1]++;
Remember we incremented a[1] before, so it is currently 2. We set j = 2, and THEN incremented it to 3. So j = 2 and now a[1] = 3.
记住我们之前增加了 a[1],所以它当前是 2。我们设置 j = 2,然后将它增加到 3。所以 j = 2 现在 a[1] = 3。
m = a[i++];
We know i = 2. So we need to set m = a[2], and then increment i. At the end of this expression, m = 15, and i = 3.
我们知道 i = 2。所以我们需要设置 m = a[2],然后增加 i。在这个表达式的末尾,m = 15,i = 3。
In summary,
总之,
i = 3, j = 2, m = 15.
回答by Kartikeya Sinha
Your understanding is not exactly correct. Pre-increment and post-increment operators are unary operators.
你的理解并不完全正确。前增量和后增量运算符是一元运算符。
So, initially if b = 5, then ++b or b++ increments the value of b to 6. However, the difference between pre and post comes when you are using an assignment operator "=".
因此,最初如果 b = 5,那么 ++b 或 b++ 会将 b 的值增加到 6。但是,当您使用赋值运算符“=”时,pre 和 post 之间的区别就出现了。
So,
所以,
if b=5
a=b++ // after this statement a=5 and b=6 as it is post increment
c=++b // after this statement c=7 and b=7
For clear understanding, you can divide the above statements as:
为了清楚地理解,您可以将上述语句划分为:
a=b;
b=b+1; //post increment
b=b+1; //pre increment
c=b;`
So, the example you gave:
所以,你给出的例子:
main( )
{
int a[5] = { 5, 1, 15, 20, 25 } ;
int i, j, k = 1, m ;
i = ++a[1] ; // a[1] = 2 and i = 2
j = a[1]++ ; // j = 2 and a[1] = 3
m = a[i++] ; // m = a[2++] = 15, i now becomes 3
printf ( "\n%d %d %d", i, j, m ) ; // so i =3, j= 2 and m =15
}
For clarity, I am splitting the above code into multiple statements:
为清楚起见,我将上述代码拆分为多个语句:
main( )
{
int a[5] = { 5, 1, 15, 20, 25 } ;
int i, j, k = 1, m ;
a[1] = a[1] + 1;
i = a[1];
j = a[1];
a[1] = a[1] + 1;
m = a[i]; // m = a[2] = 15
i = i + 1;
printf ( "\n%d %d %d", i, j, m ) ; // so i =3, j= 2 and m =15
}
I hope the above explanation clears your doubt and the output of the program you are running.
我希望上面的解释可以消除您的疑问以及您正在运行的程序的输出。
回答by Setu Kumar Basak
Explanation:
解释:
Step 1:int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
第一步:int a[5] = {5, 1, 15, 20, 25}; 变量 arr 被声明为一个大小为 5 的整数数组,它被初始化为 a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4 ] = 25。
Step 2:int i, j, m; The variable i,j,m are declared as an integer type.
第二步:int i, j, m; 变量 i,j,m 被声明为整数类型。
Step 3:i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
第 3 步:i = ++a[1]; 变成 i = ++1; 因此 i = 2 和 a[1] = 2
Step 4:j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
第 4 步:j = a[1]++;变成 j = 2++; 因此 j = 2 和 a[1] = 3。
Step 5:m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
第五步:m = a[i++]; 变成 m = a[2]; 因此 m = 15 并且 i 增加 1(i++ 表示 2++ 所以 i=3)
Step 6:printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
第六步:printf("%d, %d, %d", i, j, m); 它打印变量 i, j, m 的值
Hence the output of the program is 3, 2, 15
因此程序的输出是 3, 2, 15

