简体   繁体   中英

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

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

According to the standard, thou shalt not use pointer arithmetic to compute addresses outside the original object (or at most one past the end) . But if you convert to an integer first, you can do whatever you want (converting back to a pointer afterwards is legal, but inadvisable. According to the standard that is an unsafely-derived pointer .)

Better:

printf("address of cname is %zx\nand of arptr is     %zx\n",
       (intptr_t)cname-trunker,
       (intptr_t)arptr-trunker
      );

But if you just want to display the last four digits, this is even better:

printf("address of cname is %04x\nand of arptr is     %04x\n",
       (unsigned)cname & 0xffffU,
       (unsigned)arptr & 0xffffU
      );

Demonstration: http://ideone.com/t8gQn

Getting rid of warnings:

(unsigned)((intptr_t)someptr & 0xffffU)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM