-1

I'm learning C and I saw this code but I can't tell the difference. Can someone explain this please ? Thanks.

#include <stdio.h>
int main(int argc, char const *argv[])
{
    int addressAsInt = 0x61FF08;
    printf("address = %p\n", addressAsInt);
    int address2AsInt = 0x61FF14;
    printf("address2 = %p\n", address2AsInt);
    printf("rest = %d\n", address2AsInt - addressAsInt);

    int* address = (int*) 0x61FF08;
    printf("address = %p\n", address);
    int* address2 = (int*)  0x61FF14;
    printf("address2 = %p\n", address2);
    printf("rest = %d\n", address2 - address);

    return 0;
}

The first difference outputs 0000000C and the second 00000003. I understand the first one but not the second.

  • 1
    Pointer arithmetic is meaningless unless they are both pointers into the same object (or for an array, just past it). – Weather Vane May 18 '19 at 13:30
  • 1
    2nd case, difference is 3 `int`s apart. – chux - Reinstate Monica May 18 '19 at 13:31
  • `addressAsInt` is an int. Printing it with `%p` invokes undefined behavior[ – phuclv May 18 '19 at 13:37
  • Pointer arithmetic works in a similar way to array indexing. When you access the second element in `int array[]` you use `array[1]` not `array[4]`. – Weather Vane May 18 '19 at 13:42
  • Possible duplicate of [subtracting two addresses giving wrong output](https://stackoverflow.com/questions/44498435/subtracting-two-addresses-giving-wrong-output) – phuclv May 18 '19 at 13:43
  • other duplicates: [Subtracting two pointers giving unexpected result](https://stackoverflow.com/q/46891748/995714), [When I subtract memory addresses, why is the result smaller than I expected?](https://stackoverflow.com/q/8357151/995714), [Pointer subtraction confusion](https://stackoverflow.com/q/3238482/995714), [When subtracting two pointers in C](https://stackoverflow.com/q/39984816/995714)... – phuclv May 18 '19 at 13:44

1 Answers1

2

The first operation is simple difference between two hexadecimals (12 = C in hexadecimal).

The second operation is difference between two pointers: it gives the number of elements of same type which can fit between respective target of the two pointers.

Michaël Randria
  • 453
  • 1
  • 5
  • 21
  • So does it mean there is place for 3 integers between these 2 pointers ? –  May 18 '19 at 13:36
  • 1
    Not between the two pointers but where the pointers point to. But you pointers does not point to an object. Is this a portion of code ? – Michaël Randria May 18 '19 at 13:39
  • Yes :) The first pointer points to the first element of an array and the second to the last element. –  May 18 '19 at 13:42
  • Thanks! I tested it with short and double and you're right. –  May 18 '19 at 13:45
  • @Minara, you have said the first pointer points to the first element and the second to the last element of an array, be careful with that, as the pointer difference will not tell you the number of elements of the array, but one less than that. For that, you have to make the second to point to the next element past the last of the array. – Luis Colorado May 20 '19 at 06:54