Address in C: &(number)
c
Solution
arr[1] - arr[4]
This is just as it looks. `7 - 5 = 2`, so let's replace that with `2`:
arr[2]
That's also just as it looks. `4`. The `&` takes its address, which will be two `int`s offset from the beginning of the array, which is `arr`.
&(arr[2]) - arr
That subtracts `arr`, so you're left with the offset of `arr[2]` from `arr`, which is two `int`s. There you go.
Here's a reduced example.
In case you were expecting it to be `8`, well, that's just how pointer arithmetic is. Casting them both to `unsigned int`:
(unsigned int)&arr[2] - (unsigned int)arr
will yield `8`, at least when an `int` is four bytes. (Demo)
Problem
I don't understand the output of this program: ``` int arr[]={1,7,4,2,5,8}; int x=(&(arr[arr[1]-arr[4]])-arr); printf("%d" ,x); ``` `arr[arr[1]-arr[4]]` is equal to 4. What does it mean `&(4)`? Why does it print 2?