Increment operator inside array
Asked Answered
S

6

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++];
Scream answered 13/12, 2015 at 12:16 Comment(5)
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.Sprague
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.Howlyn
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?Scream
So you don't know how array indexing works? Read a book of follow a tutorial.Sprague
++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.Howlyn
M
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.

Middendorf answered 13/12, 2015 at 12:25 Comment(2)
So, this is normal operation. It won't affect the array elements right?Scream
No, it won't - elements, not indices.Middendorf
G
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.

Gretagretal answered 13/12, 2015 at 12:35 Comment(0)
P
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.

Piggott answered 13/12, 2015 at 12:38 Comment(0)
Z
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

Zelig answered 13/12, 2015 at 12:31 Comment(0)
R
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.

Roister answered 13/12, 2015 at 14:24 Comment(0)
B
-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.

Burschenschaft answered 16/6, 2023 at 3:48 Comment(2)
Irrelevant answer on a 7+ year old question.Boole
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.Araby

© 2022 - 2024 — McMap. All rights reserved.