[email protected]:~$ cat /tmp/test.c
#include<stdio.h>
int main(void)
{
int m=1;
printf("m=%d\n",&m);
printf("m=%p\n",&m);
}
[email protected]:~$ gcc /tmp/test.c -o /tmp/test.exe
[email protected]:~$ /tmp/test.exe
m=-1078061268
m=0xbfbe172c
[email protected]:~$ python
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print hex(-1078061268)
-0x4041e8d4
爲什麼-1078061268
在dec中不等於0xbfbe172c
in hex?爲什麼地址編號在十進制和十六進制中不相等?
這些數字是相等的。由於sign屬性和不同的基數(16 vs 10),十六進制值看起來與unsigned int不同。 – tay10r
除了識別十進制與十六進制模式的問題之外,還存在更嚴重的問題。永遠不要使用'%d'打印指針。整數和指針通常具有不同的寬度,所以'printf'會嚴重混淆它的參數,導致程序崩潰。 –