<分区>
我只是写了一个简单的程序来打印变量的地址。
#include <stdio.h>
void main(){
int *a;
int b;
b=5;
a=&b;
printf("\n*a is the value of the integer: %d\n",*a);
printf("&a is the address of variable a: %p\n", &a);
printf(" a is the address stored in a: %p\n",a);
printf("&b is the address of variable b: %p\n\n",&b);
}
结果是:
*a is the value of the integer: 5
&a is the address of variable a: 0x7fff935ad2b0
a is the address stored in a: 0x7fff935ad2bc
&b is the address of variable b: 0x7fff935ad2bc
为什么打印的地址不是标准格式?
抱歉这个愚蠢的问题,但我就是不明白。
编辑
我有另一个程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct small{
int a;
};
int main(){
struct small *sm;
sm = malloc(sizeof(struct small));
memset(sm,0,sizeof(struct small));
sm->a = 5;
printf("The address of sm is: %p\n", &sm);
printf("The address stored in sm is: %p\n", sm);
return 0;
}
输出是:
The address of sm is: 0x7fffd1363158
The address stored in sm is: 0x17a3010
所以我希望格式 0x17a3010 对我来说有点标准。
为什么在这种情况下 %p 的格式不同?