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++];
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++];
Please explain these operations.
array[++i];
- first increments i
, then gives you element at the incremented index
equivalent to:
++i; // or i++
array[i];
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.
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.
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.
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
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.
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.
© 2022 - 2024 — McMap. All rights reserved.
++i
andi++
do? If not, read a book of follow a tutorial. – Spraguearray[++i]
pre-incrementsi
, then will accessarray[i]
.array[i++]
will accessarray[i]
then post-incrementi
. These are basic cases for pre- and post-increment. You can write a very simple test program to demonstrate this. – Howlyn++i
ori++
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