1

I have this below code:

main()
{
    int pt = {0}, *ppt = &pt;
    char *p1 = NULL, *p2 = NULL;

    p1 = (char*)(ppt);
    p2 = (char*)(ppt+1);
    printf("%p\n",p1);
    printf("%p\n",p2);
    printf("%d",(p2-p1));
}

This gives me the size of the variable as expected. But I don't understand how this works. The p1 and p2 as to be char. Why? If I change that to short or int, I get the result as 1, though the address has the same difference.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
jxgn
  • 741
  • 2
  • 15
  • 36
  • Read more your C programming books about [pointer arithmetic](http://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm) – Basile Starynkevitch Sep 28 '14 at 15:33
  • what does `int pt = {0}` mean? – Jason Hu Sep 28 '14 at 15:33
  • I don't see a reason why p1 and p2 must be char *. Should work with any other type also. – Thallius Sep 28 '14 at 15:34
  • @HuStmpHrrr: Same as `int pt = 0;`. The syntax with the redundant pair of `{}` has been in the language since forever. This is why `{ 0 }` is an universal zero initializer in C. You can use `{ 0 }` to initialize anything. – AnT stands with Russia Sep 28 '14 at 15:35
  • 1
    @claus: you will not get the size unless you switch to char* arithmetic. – Oliver Charlesworth Sep 28 '14 at 15:36
  • You have to remember that for any pointer `p` and any index `i`that `p[i]` is equivalent to `*(p + i)`. That should help explain why the result you get is the difference in *units* and not bytes. – Some programmer dude Sep 28 '14 at 15:37
  • @Oliver I dont understand. ppt will give me back the memory address of the int pt. Just for example lets say this is 0x00000000. (ppt+1) will give me back the memory address from ppt + one int (for 32bit int) this will be 0x00000004. Therefor it is totally similar if I cast the address to p1 = (long*)(ppt) oder p1=(long long *). The values will always be 0x00000000 and 0x00000004 because *p1 and *p2 will always be a pointer regardingless of which type. So p2-p1 will always be 4 whatever p1 or p2 are defined as. – Thallius Sep 28 '14 at 15:46
  • 1
    @Claus Bönnhoff: "... will always be 4 whatever p1 or p2 are defined as" - that's false. One more time: you need to read about pointer arithmetic in C. In C pointer arithmetic takes into account pointer type. It works in therms of pointed type size. If the pointed type has size `4`, then `0x4 - 0x0` will produce `1`, not `4`. The absolute difference between addresses is automatically *divided* by the pointed type size. This is why we always use `char *` (since `char` always has size `1`) for raw pointer arithmetic. – AnT stands with Russia Sep 28 '14 at 15:48
  • I'm embarrassed. Perhaps I should not try to answer questions directly coming home from a 3 day developer conference. Sorry Claus – Thallius Sep 28 '14 at 15:56
  • @AndreyT Please make the reply to clause as the answer. The point that address is automatically divided by the pointed type size helped :) which really says why it has to be a char Thanks :) – jxgn Sep 28 '14 at 16:21
  • @Xavier Geoffrey: It is no longer possible to reply to your question since it is marked as duplicate. I think the answer at the duplicate link are sufficiently clear in this. – AnT stands with Russia Sep 28 '14 at 16:24
  • @AndreyT oh ok...but still i couldn't understand before i read your answer... – jxgn Sep 28 '14 at 16:27

0 Answers0