14

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++];
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Loki San Hitleson
  • 177
  • 1
  • 2
  • 7
  • 2
    Could you be more specific about what you don't understand? Do you know what `++i` and `i++` do? If not, read a book of follow a tutorial. – juanchopanza Dec 13 '15 at 12:18
  • 2
    `array[++i]` *pre-increments* `i`, then will access `array[i]`. `array[i++]` will access `array[i]` then *post-increment* `i`. These are basic cases for pre- and post-increment. You can write a very simple test program to demonstrate this. – lurker Dec 13 '15 at 12:22
  • I know the difference of ++i and i++ . But want the use of inside array operation. Is this shifts all array elements to next one? – Loki San Hitleson Dec 13 '15 at 12:23
  • 1
    So you don't know how array indexing works? Read a book of follow a tutorial. – juanchopanza Dec 13 '15 at 12:25
  • `++i` or `i++` don't do any hidden operations (such as "shift") with your array. They do exactly as I described in my last comment. However, they could be part of code within a loop to perform such a shift. – lurker Dec 14 '15 at 17:05

6 Answers6

21

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.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
5

The ++i increments i before evaluating it.

The i++ inrements i after evaluating it.

If i=1 then array[++i] sets i=2 and then fetches array[2].

If i=1 then array[i++] fetches array[1] then sets i=2.

The post- and pre- operations happen after or before the expression they are involved in is evaluation.

I generally discourage the use of post and pre increment operators in expressions. They can lead to confusion at best and bugs at worst.

Consider what x = array[++i] + array[i--] ; should be. See how easy it is to confuse the programmer ( or the poor devil who has to fix your code ? :-) ).

Post and pre increment and decrement operations can also produce problems in macros, as you end up with the potential for an operation to be duplicated multiple times, especially with macros.

It is simpler and produces easier to maintain code to avoid post and pre increments in expressions, IMO.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
3

So, you know i++ and ++i increment i with 1. Also, this instruction returns i, so you can put this somewhere in your code where you need the value of i.

The difference between the 2 is that i++ is post increment, and ++i is pre increment. What does this mean?

Well, let's say i is 6. When you do:

array[i++]
array[i]

You will actually be doing:

array[6]
array[7]

Because you use post increment: first return value, then increment i.

If you do:

array[++i]
array[i]

You'll basically be doing:

array[7]
array[7]

Because you use pre increment: first increment i, then return its value.

Now try to find what your code does ;-)

Hope this helps.

regapictures
  • 371
  • 3
  • 12
1

array[++i]; - increments the value of i and then uses the incremented value as an index into array

array[i++]; -indexes into the array and then increments the value of i

jeremy
  • 21
  • 2
0

I know your question was in the context of a queue, but I'm going to use a stack for illustration.

Imagine an array-based stack:

T stack[N];     // for some type T
size_t sp = N;  // stack pointer

In this example, the stack grows "downwards", where index N-1 is the bottom of the stack and index 0 is the top.

A push operation looks like the following:

stack[--sp] = val;

--sp evaluates to sp - 1, and as a side effect decrements the value in sp, so the above statement is equivalent to writing

stack[sp - 1] = val;
sp = sp - 1;

with the caveat that sp may be updated before the assignment is complete.

A pop operation looks like this:

val = stack[sp++];

sp++ evaluates the the current value of sp, and as a side effect increments the value in sp, so it's equivalent to writing

val = stack[sp];
sp = sp + 1;

with the same caveat as above.

John Bode
  • 119,563
  • 19
  • 122
  • 198
-1

Are you trying to use ++ as a math operator, wouldn't c++ just try to use it as a math operator instead of incrementing it because it is inside of the square bracket operators, and you can't use ++ as a math operator. ++ is just used to increment a variables and not used as a math operator.

  • Irrelevant answer on a 7+ year old question. – panic Jun 16 '23 at 13:54
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 16 '23 at 15:45