For i=4,

You took sum = 7 while in i=3 sum is 6

You took sum = 7 while in i=3 sum is 6

Dark Mode

10,521 views

26 votes

Best answer

6 votes

#include <stdio.h> int main() { int a[] = {2, 4, 6, 8, 10}; int i, sum=0, *b=a+4; /* Here b points to the address of a[4]=10 */ /* *b means the value of which the address b points to. */ /* So *b = a[4] = 10 */ /* (b-1) means &a[4-1], so *(b-1)=a[4-1]=8 */ /* (b-2) means &a[4-2], so *(b-2)=a[4-2]=6 */ /* and so on. */ for (i=0; i<5; i++) sum=sum+(*b-i)-*(b-i); /* sum=0+(10-0)-(10) = 0 */ /* sum=0+(10-1)-(8) = 1 */ /* sum=1+(10-2)-(6) = 3 */ /* sum=3+(10-3)-(4) = 6 */ /* sum=6+(10-4)-(2) = 10 */ printf("%d\n", sum); /* It will give the output as 10. */ return 0; }

So the correct answer is $10$.

@Sachin Mittal 1sum=sum+(*b-i) - *(b-i) in this line how this ** *(b-i) will be zero. *(10-0) = *10 but at 10 place there is no value so 0 10-0=10 where i am wrong?**

0

@jugnu1337 $\ *b = a+4 \text{ i.e.pointing to }10$, hence *b is **not *10 but only 10. **

Now, in *(b-i), here b’s address is subtracted with 0, leaving only b’s address. Thus ***(b-i) = 10 not *10**.

1