Search code examples
c++carrayspost-incrementpre-increment

Increment operator inside array


I have a C program which does queue operations using an array. In that program, they increment a variable inside array. I can't understand how that works. So, please explain these operations:

array[++i];
array[i++];

Solution

  • Please explain these operations.

    1. array[++i]; - first increments i, then gives you element at the incremented index

      equivalent to:

      ++i; // or i++
      array[i];
      
    2. array[i++]; - also first increments i, but postfix operator++ returns i's value before the incrementation

      equivalent to:

      array[i];
      ++i; // or i++
      

    They increment a variable inside array.

    No, they don't. You could say they increment i within the call to array subscript operator.