为了清楚起见想要截断地址,但没有按预期工作
为了清楚起见,想要截断地址。 不寻找改进的代码, 这就是为什么十六进制代码减法似乎无法正常工作。
从“cname”和“arptr”中减去“零”的第一个结果:
address of 'cname' is 0x7fff5fbff720
and of 'arptr' is 0x7fff5fbff720
为了清楚起见,想要截断不需要的数字 我减去 0x7fff5fbf0000 我想我会得到 0x00000000f720,但是:
从“cname”和“arptr”中减去“trunker”的第二个结果:
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;
}
在 OS X 上使用 Xcode
谢谢,2012 年新年快乐
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
指针算术是以所指向的事物为单位执行的,而不是以字节/字符为单位。
你可以试试这个:
Pointer arithmetic is performed in units of the thing being pointed to, not in units of bytes/chars.
You could try this instead:
根据该标准,不得使用指针算术来计算原始对象之外的地址(或最多超过末尾的地址)。但如果你先转换为整数,你可以做任何你想做的事情(之后转换回指针是合法的,但不可取。根据标准,这是一个不安全派生的指针。)
更好:
但如果您只想显示最后四位数字,这甚至更好:
演示:http://ideone.com/t8gQn
摆脱警告:
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:
But if you just want to display the last four digits, this is even better:
Demonstration: http://ideone.com/t8gQn
Getting rid of warnings: