马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
int main(void)
{
char a = 0, b = 0;
// printf("a->%p, b->%p\n",&a,&b);
int *p = (int *)&b;
// printf("p->%p, p+1->%p\n",p,p+1);
*p = 258;
// printf("p->%p, p+1->%p\n",p,p+1);
// printf("%d\n",*p);
printf("a = %d, b = %d\n",a,b);
printf("sizeof(int) = %d\n",sizeof(int));
printf("sizeof(char) = %d\n",sizeof(char));
return 0;
}
输出为: a = 0, b = 2
sizeof(int) = 4
sizeof(char) = 1
如果先打印了两个变量和指针的地址:#include<stdio.h>
int main(void)
{
char a = 0, b = 0;
printf("a->%p, b->%p\n",&a,&b);
int *p = (int *)&b;
printf("p->%p, p+1->%p\n",p,p+1);
*p = 258;
printf("p->%p, p+1->%p\n",p,p+1);
// printf("%d\n",*p);
printf("a = %d, b = %d\n",a,b);
printf("sizeof(int) = %d\n",sizeof(int));
printf("sizeof(char) = %d\n",sizeof(char));
return 0;
}
输出为:a->000000000061FE17, b->000000000061FE16
p->000000000061FE16, p+1->000000000061FE1A
p->0000000000610000, p+1->0000000000610004
a = 1, b = 2
sizeof(int) = 4
sizeof(char) = 1
已经理解了为什么a=1,b=2.
请问为什么在不打印a,b的地址的时候a=0,b=2呢?
以及为什么在给p赋值之后,p指向的地址被改变了?
感谢! |