# How is this number calculated?

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)

``````

Output:

``````Test1: 0x20000000
Test2: 0x20000004
Test3: 0x20000008
``````
• What language? Or should we be guessing? – Oded Sep 2 '12 at 19:48
• I wasn't aware the language was relevant, but if so, the language I'm working with is C. – Hamid Sep 2 '12 at 19:49
• It certainly is. Different languages have different literal semantics. – Oded Sep 2 '12 at 19:50
• I see. Thanks for the head up. In that case, you should take little of what I have in the code blocks above as being literal. It's an arbitrary representation I constructed for the question. – Hamid Sep 2 '12 at 19:52
• What is the data type of `address1`? If `address1` is a pointer which points to something whose sizeof happens to be 4 bytes on your platform, then `address1 + 1` will calculate an address value offset from address1 by 4 bytes. (Note that int is often 4 bytes in size on a 32-bit desktop platform) – Ben Cottrell Sep 2 '12 at 20:07

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`).

• When you add a value to a pointer, it is incremented by the number of chars necessary to store the type pointed to. This could be 1,2,4,... chars (or, indeed, even 3 chars, but that is rather unlikely on modern machine). – Vatine Sep 3 '12 at 12:00
• @Vatine Indeed, I should have mentioned that the `4` is a consequence of the pointer being a pointer to `int`; instead I preferred leaving the answer as concise as possible, but you're right. – Nadir Sampaoli Sep 3 '12 at 22:36
• @Vatine: Adding an integer N to a pointer gives you a new pointer value advanced N elements past the location to which the original pointer points. Saying that adding N to a pointer actually adds `N * sizeof (whatever)` to the pointer implies that pointers are really just integers; they're not. – Keith Thompson Sep 4 '12 at 2:57
• @KeithThompson: You are correct. I made a short, maybe not as clear as it could have been, clarification to an accepted answer talking about "addresses". – Vatine Sep 4 '12 at 8:10