1 #include <stdio.h>
2 void main()
3 {
4 int i=4;
5 int*p=&i;
6 //int *p=(int *)4;
7 printf("%d\n",*p);
8 printf("%p\n",p);
9 *p=12;
10 printf("%d\n",*p);
11 printf("%p\n",p);
12 getchar();
13 return;
14 }
当为 int *p=(int *)4的时候会报错。
因为 4在内存中没有相应地址映射,p指向的是非法地址。
ps:在对指针操作时,如果拿不住,还是if()一下比较安全。