printf("address of &tmp is= %p\n\n", &tmp); printf("address of tmp->foo= %p \t offset of tmp->foo= %lu\n", &tmp.foo, (unsignedlong) &((struct foobar *)0)->foo); printf("address of tmp->bar= %p \t offset of tmp->bar= %lu\n", &tmp.bar, (unsignedlong) &((struct foobar *)0)->bar); printf("address of tmp->boo= %p \t offset of tmp->boo= %lu\n\n", &tmp.boo, (unsignedlong) &((struct foobar *)0)->boo);
printf("computed address of &tmp using:\n"); printf("address and offset of tmp->foo= %p\n", (struct foobar *) (((char *) &tmp.foo) - ((unsignedlong) &((struct foobar *)0)->foo))); printf("address and offset of tmp->bar= %p\n", (struct foobar *) (((char *) &tmp.bar) - ((unsignedlong) &((struct foobar *)0)->bar))); printf("address and offset of tmp->boo= %p\n", (struct foobar *) (((char *) &tmp.boo) - ((unsignedlong) &((struct foobar *)0)->boo))); return0; }
编译运行得到如下结果:
1 2 3 4 5 6 7 8 9 10
address of &tmp is= 0x7ffcf635c9b0
address of tmp->foo= 0x7ffcf635c9b0 offset of tmp->foo= 0 address of tmp->bar= 0x7ffcf635c9b4 offset of tmp->bar= 4 address of tmp->boo= 0x7ffcf635c9b5 offset of tmp->boo= 5
computed address of &tmp using: address and offset of tmp->foo= 0x7ffcf635c9b0 address and offset of tmp->bar= 0x7ffcf635c9b0 address and offset of tmp->boo= 0x7ffcf635c9b0