I have numbers;
A == 0x20000000
B == 18
C == (B/10)
D == 0x20000004 == (A + C)
A and D are in hex, but I’m not sure what the assumed numeric bases of the others are (although I’d assume base 10 since they don’t explicitly state a base.
It may or may not be relevant but I’m dealing with memory addresses, A and D are pointers.
The part I’m failing to understand is how 18/10 gives me 0x4.
Edit: Code for clarity:
*address1 (pointer is to address: 0x20000000)
printf("Test1: %pn", address1);
printf("Test2: %pn", address1+(18/10));
printf("Test3: %pn", address1+(21/10));
Output:
Test1: 0x20000000
Test2: 0x20000004
Test3: 0x20000008
11
Notice some facts:
1) when you add a value to the address it gets increased by that value multiplied by the number of bytes contained in a word, not by simply that value;
2) 18/10 == 1
when it comes to integers;
3) 21/10 == 2
when it comes to integers;
4) word size is 4 in this case (as you notice by the pointer’s size, being 32 bit);
Consequently:
0x20000000 + 4 * (18/10) = 0x20000000 + 4 * 1 = 0x20000004
0x20000000 + 4 * (21/10) = 0x20000000 + 4 * 2 = 0x20000008
Edit:
As Vatine pointed out, it’s important understanding that the pointer is incremented by a value multiplied by 4 (i.e a word’s size in a 32-bit system) because that’s the size of the data type the pointer variable was created for (an int
).
4