16

What is the difference between array[i]++ (increment outside brackets) and array[i++] (increment inside brackets), where the array is an int array[10]?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
samprat
  • 2,150
  • 8
  • 39
  • 73

7 Answers7

31
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.

IHS
  • 3
  • 1
  • 5
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • 2
    What's missing from this answer is what `a[i]++` and `a[i++]` evaluate to. – David Heffernan Sep 29 '11 at 09:56
  • It is a little bit confusing because the array *values* are in about the same range as the array *indexes*. Can you offset the values (say, by 60), so there isn't any overlap between indexes and values? – Peter Mortensen Jan 13 '20 at 03:38
  • 2
    The example output is misleading, since the access of `a[i++]` in the second statement actually returns the value at index `1`, which has been inceremented to `3` before. Be aware that `x = a[i++];` is equivalent to the statement sequence `x = a[i]; i++;` So i is used as index *before* it is incremented. – Richard Neumann Jan 24 '21 at 18:17
25
  • array[i]++ increments the value of array[i]. The expression evaluates to array[i] before it has been incremented.
  • array[i++] increments the value of i. The expression evaluates to array[i], before i has been incremented.

An illustration.

Suppose that array contains three integers, 0, 1, 2, and that i is equal to 1.

  • array[i]++ changes array[1] to 2, evaluates to 1 and leaves i equal to 1.
  • array[i++] does not modify array, evaluates to 1 and changes i to 2.

A suffix operators, which you are using here, evaluates to the value of the expression before it is incremented.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
5

array[i]++ means ( *(array+i) )++. --> Increments the Value.

array[i++] means *( array + (i++) ). --> Increments the Index.

Sadique
  • 22,572
  • 7
  • 65
  • 91
2

Here the Array[i]++ increments the value of the element array[i], but array[i++] increments the i value which effects or changes the indication of the array element (i.e. it indicates the next element of an array after array[i]).

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
Gouse Shaik
  • 340
  • 1
  • 2
  • 15
1

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.

mel
  • 19
  • 3
0

Here array[i++] increments the index number.
On the contrary, array[i]++ increments the data value of i index.

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;
}
rashedcs
  • 3,588
  • 2
  • 39
  • 40
0
#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. It then increases the value from 40 to 41 .So arr[i]++ increments the value of this particular index and a[i++] first increments the index and then gives the value for this index.

W D
  • 204
  • 1
  • 10
Tanim_113
  • 321
  • 1
  • 3
  • 11