- 别说我没告诉过你指针匹配类型有多重要,指针类型决定了从开始地址取多长,如何解析
void main() { int num = 100; double *p = (double*)#//明明是整型,我非tm按照double解析 printf("指针p指向%x,该地址上存的值为%f ",p,*p); getchar(); }
unfortunately
- 指针的运算
p=&a将变量a地址赋予指针p p=&a[i]将数组元素地址赋给指针p p=a[10]将数组首元素地址赋给指针p p1=p2指针p2赋值给指针p1
#include<stdio.h> #include<stdlib.h> void main() { int a[10] = {0,1,2,3,4,5,6,7,8,9}; for (int i = 0; i<10; i++) { printf("a+i=%x,&a[i]=%x ", a + i, &a[i]); } getchar(); }
输出结果:
不难得出,a即代表首元素地址a+i意味着在首地址的基础上推进i个元素a+i=&a[i]=======>*(a+i)=a[i]
所以一种很拽的写法是
#include<stdio.h> #include<stdlib.h> void main() { int a[10] = {0,1,2,3,4,5,6,7,8,9};for (int *p = a; p < a + 10; p++) { *p = 9 - *p; printf(" %d", *p); } getchar(); }
输出结果: