What does this mean *((int*)(&val) +1)

Hello,

I'm trying to understand this line of code. Can someone help me? Is it saving the result in the variable val or in the address of the variable val?

 
  *((int*)(&val) +1)= A*(y) + (B - C)

Last edited on
Neither. It treats val as an integer pointer, offsets it by one, obtains a reference to the pointed-to memory location, and copy-assigns the right-hand side to that referent.

Let's look at the lvalue expression -- the stuff on the left:
*((int*)(&val) +1)
We know from parentheses and the precedence rules which happens in what order. Starting from the inside,

(&val) Take the address of val.
(int*)(&val) Treat it as an integer pointer.
((int*)(&val) +1) Advance to address the next integer.
*((int*)(&val) +1) Obtain a reference to the next integer to val.

The referent then gets assigned the value of the right-hand side.
Last edited on
You did not show the type of val. It does not affect the explanation, but one would like to know why the explicit cast to int* is used.
Topic archived. No new replies allowed.