Pre increment vs Post increment in array

本秂侑毒 提交于 2019-11-27 18:50:30

You hit the nail on the head. Your understanding is correct. The difference between pre and post increment expressions is just like it sounds. Pre-incrementation means the variable is incremented before the expression is set or evaluated. Post-incrementation means the expression is set or evaluated, and then the variable is altered. It's easy to think of it as a two step process.

b = x++;

is really:

b = x;
x++;

and

b = ++x;

is really:

x++;
b = x;

EDIT: The tricky part of the examples you provided (which probably threw you off) is that there's a huge difference between an array index, and its value.

i = ++a[1];

That means increment the value stored at a[1], and then set it to the variable i.

m = a[i++];

This one means set m to the value of a[i], then increment i. The difference between the two is a pretty big distinction and can get confusing at first.

Second EDIT: breakdown of the code

{ 
 int a[5] = { 5, 1, 15, 20, 25 } ; 
 int i, j, k = 1, m ; 
 i = ++a[1] ; 
 j = a[1]++ ; 
 m = a[i++] ; 
 printf ( "\n%d %d %d", i, j, m ) ; 
}

First:

i = ++a[1];

At this point we know a[1] = 1 (remember arrays are zero indexed). But we increment it first. Therefore i = 2.

j = a[1]++;

Remember we incremented a[1] before, so it is currently 2. We set j = 2, and THEN incremented it to 3. So j = 2 and now a[1] = 3.

m = a[i++];

We know i = 2. So we need to set m = a[2], and then increment i. At the end of this expression, m = 15, and i = 3.

In summary,

i = 3, j = 2, m = 15.

Your understanding is not exactly correct. Pre-increment and post-increment operators are unary operators.

So, initially if b = 5, then ++b or b++ increments the value of b to 6. However, the difference between pre and post comes when you are using an assignment operator "=".

So,

if b=5
a=b++ // after this statement a=5 and b=6 as it is post increment
c=++b // after this statement c=7 and b=7

For clear understanding, you can divide the above statements as:

a=b;  
b=b+1; //post increment  
b=b+1; //pre increment  
c=b;`  

So, the example you gave:

main( )    
{      
 int a[5] = { 5, 1, 15, 20, 25 } ;     
 int i, j, k = 1, m ;  
 i = ++a[1] ; // a[1] = 2 and i = 2  
 j = a[1]++ ; // j = 2 and a[1] = 3  
 m = a[i++] ; // m = a[2++] = 15, i now becomes 3  
 printf ( "\n%d %d %d", i, j, m ) ; // so i =3, j= 2 and m =15  
}

For clarity, I am splitting the above code into multiple statements:

main( )    
{      
 int a[5] = { 5, 1, 15, 20, 25 } ;     
 int i, j, k = 1, m ;
 a[1] = a[1] + 1;  
 i = a[1];  
 j = a[1];  
 a[1] = a[1] + 1;  
 m = a[i]; // m = a[2] = 15  
 i = i + 1;  
 printf ( "\n%d %d %d", i, j, m ) ; // so i =3, j= 2 and m =15  
}

I hope the above explanation clears your doubt and the output of the program you are running.

Explanation:

Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .

Step 2: int i, j, m; The variable i,j,m are declared as an integer type.

Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2

Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.

Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)

Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m

Hence the output of the program is 3, 2, 15

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!