chextruncatepointer-address

Wanting to truncate address for clarity not working as expected


Wanting to truncate addresses for clarity. Not looking for improved code, just why the hex code subtraction does't seem to work properly.

First result with 'zero' being subtracted from 'cname' and 'arptr':

address of 'cname' is 0x7fff5fbff720
and of 'arptr' is     0x7fff5fbff720

Wanting to truncate unneeded digits for clarity I subtract 0x7fff5fbf0000 thinking I'll get 0x00000000f720, but:

Second result with 'trunker' being subtracted from 'cname' and 'arptr':

address of 'cname' is 0xffff00014082f720
and of 'arptr' is     0xffff00014082f720
#include <stdio.h>
#define trunker 0x7fff5fbf0000
#define zero    0x000000000000

int main(void) {

    char cname[3][3] = {'a','b','c','e','f','g','i','j','k'};
    char (* arptr)[3];

    arptr = cname;

    printf("address of cname is %p\nand of arptr is     %p\n",
    cname-zero,arptr-zero);   //replace zero with trunker 
                              //to truncate first 8 digits

    return 0;

}

Using Xcode on OS X

Thanks and Happy New Year 2012


Solution

  • Pointer arithmetic is performed in units of the thing being pointed to, not in units of bytes/chars.

    You could try this instead:

    printf("address of cname is %p\nand of arptr is     %p\n",
        (char*)cname-zero, (char*)arptr-zero);