C语言 C 中的数组增量类型 - array[i]++ vs array[i++]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7595247/
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
Array increment types in C - array[i]++ vs array[i++]
提问by samprat
What is the difference between array[i]++and array[i++], where the array is an int array[10]?
array[i]++和之间有什么区别array[i++],其中数组是 int array[10]?
回答by taskinoor
int a[] = {1, 2, 3, 4, 5};
int i = 1; // Second index number of the array a[]
a[i]++;
printf("%d %d\n", i, a[i]);
a[i++];
printf("%d %d\n", i, a[i]);
Output
输出
1 3
2 3
a[i]++increments the element at index i, it doesn't increment i. And a[i++]increments i, not the element at index i.
a[i]++递增 index 处的元素i,它不会递增i。并且a[i++]递增i,而不是 index 处的元素i。
回答by David Heffernan
array[i]++increments the value ofarray[i]. The expression evaluates toarray[i]before it has been incremented.array[i++]increments the value ofi. The expression evaluates toarray[i], beforeihas been incremented.
array[i]++增加 的值array[i]。表达式array[i]在增加之前计算为。array[i++]增加 的值i。表达式的计算结果为array[i], 之前i已递增。
An illustration.
一个插图。
Suppose that arraycontains three integers, 0, 1, 2, and that iis equal to 1.
假设它array包含三个整数,0、1、2,并且i等于 1。
array[i]++changesarray[1]to 2, evaluates to 1 and leavesiequal to 1.array[i++]does not modifyarray, evaluates to 1 and changesito 2.
array[i]++更改array[1]为 2,计算结果为 1 并离开i等于 1。array[i++]不修改array,计算结果为 1 并更改i为 2。
A suffix operators, which you are using here, evaluates to the value of the expression before it is incremented.
您在此处使用的后缀运算符计算表达式的值,然后再递增。
回答by Sadique
array[i]++means ( *(array+i) )++. --> Increments the Value.
array[i]++是指( *(array+i) )++。--> 增加值。
array[i++]means *( array + (i++) ). --> Increments the Index.
array[i++]是指*( array + (i++) )。--> 增加索引。
回答by Gouse Shaik
Here the Array[i]++increments the value of the element array[i],
but array[i++]increments the ivalue which effects or changes the indication of the array element (i.e. it indicates the next element of an array after array[i]).
这里Array[i]++增加了元素的值array[i],但array[i++]增加了i影响或改变数组元素指示的值(即它指示数组的下一个元素之后array[i])。
回答by rashedcs
Here array[i++]increments the index number.
On the contrary, array[i]++increments the data value of iindex.
这里array[i++]增加索引号。
相反,array[i]++增加i索引的数据值。
Code Snippet:
代码片段:
#include <iostream>
using namespace std;
int main()
{
int array[] = {5, 2, 9, 7, 15};
int i = 0;
array[i]++;
printf("%d %d\n", i, array[i]);
array[i]++;
printf("%d %d\n", i, array[i]);
array[i++];
printf("%d %d\n", i, array[i]);
array[i++];
printf("%d %d\n", i, array[i]);
return 0;
}
回答by Tanim_113
#include<iostream>
using namespace std;
int main()
{
int arr[]={1,2,37,40,5,7};
int i = 3;
arr[i]++;
cout<<i<<" "<<arr[i]<<endl;
arr[i++];
cout<<i<<" "<<arr[i]<<endl;
return 0;
}
output:
输出:
3 41
4 5
In this example i = 3 so,arr[3]= 40 and then it increases the value 40 to 41.So arr[i]++ increments the value of this particular index and a[i++] first increment the index and then gives the values of this index.
在这个例子中 i = 3 所以,arr[3]= 40 然后它将值 40 增加到 41。所以 arr[i]++ 增加这个特定索引的值,a[i++] 首先增加索引然后给出该指数的值。
回答by mel
Let's say we have this example, array[i++] = x[m++]. This means that first set array[i] = x[m]then increase the indexes like i + 1, m + 1.
假设我们有这个例子,array[i++] = x[m++]. 这意味着首先设置array[i] = x[m]然后增加索引,如i + 1, m + 1。

