If you set a pointer to the address of a variable and then do a dereference assignment, you will change the value at the pointer, not the address itself. So if you write ip += 1
that changes the address, and *ip +=1
changes the value at the address.
Here's a bunch of examples that should help make it clear how pointers work, including with floating point values. You should read about the IEEE754 representation of 32 bit floats to understand better why 1.0
is represented as 0x3f800000
.
Please note that this code makes a lot of assumptions about the sizes of types (which are implementation defined!) and disregards aliasing rules (it is not allowed to point to an object in memory with a pointer type that does not match the declared type of the object and then dereference that pointer). That said, in practice, you can always interpret any memory as bits, even if it is "unsafe" or "illegal".
int x = 0;
int* ip = &x;
*ip += 1; // x = 1
ip += 1; // ip is now increased by sizeof(int) and probably points to
// the gap in the stack between x and ip, since sizeof(x) is probably 4
// and ip is probably 8 byte aligned
ip += 1; // now ip probably points to itself! (the address points to its own address value)
ip -= 2; // now ip points to x again
*ip += 2; // now x = 3
float *fp = (float*)ip; // the value *fp is the same in binary, 0b11
int z = *(int *)fp; // z is 3, because we're preventing the conversion
*fp = 1.0; // in hex, *fp is 0x3f800000
int y = *fp; // 1.0 is converted to int and stored in y as 1
int a = *(int *)fp; // a is 0x3f800000