1

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: %p\n", address1);
printf("Test2: %p\n", address1+(18/10));
printf("Test3: %p\n", address1+(21/10));

Output:

Test1: 0x20000000
Test2: 0x20000004
Test3: 0x20000008
Hamid
  • 145
  • 7
  • 1
    What language? Or should we be guessing? – Oded Sep 02 '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 02 '12 at 19:49
  • 3
    It certainly is. Different languages have different literal semantics. – Oded Sep 02 '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 02 '12 at 19:52
  • There must be something else going on that's not shown here. What's the code that produced this symptom? – Wayne Conrad Sep 02 '12 at 19:54
  • @WayneConrad I've updated the question. – Hamid Sep 02 '12 at 20:03
  • 2
    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 02 '12 at 20:07
  • @BenC Adding sizeof address1 gave me 0x4. It seems you've answered the question with yours. Thanks. EDIT: It's defined simply as "unsigned" making it an int as you suggest. – Hamid Sep 02 '12 at 20:12
  • @Hamid note that `sizeof(address1)` will give you the sizeof the pointer (which may also happen to be 4 bytes). you should try `sizeof(*address)` instead for the pointed-to type; this will likely change between `char*` , `short*`, `long*`, `double*` etc. – Ben Cottrell Sep 02 '12 at 20:15
  • @BenC Thank you very much for clarifying. In my case they are the same (according to tests) and both return 0x4. – Hamid Sep 02 '12 at 20:17
  • Following on from @nadirs answer, this shows the importance of understanding the data types you are using, especially when making use of indirection. Mistakes using pointers are a very common cause of error in C (and related languages) – Andrew Sep 02 '12 at 20:44

1 Answers1

10

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

Nadir Sampaoli
  • 420
  • 3
  • 10
  • 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 03 '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 03 '12 at 22:36
  • 1
    @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 04 '12 at 02: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 04 '12 at 08:10