1 int x, y, *px = &x, *py = &y; 2 y = *px + 5; //表示把x的内容加5并赋给y,*px+5相当于(*px)+5 3 y = ++*px; //px的内容加上1之后赋给y,++*px相当于++(*px) 4 y = *px++; //相当于y=(*px)++,然后p++,先去指针p指向的数据,然后指针++ 5 py = px; //把一个指针的值赋给另一个指针
6 *p++; 应该理解为 *(p++);
以下可以指针遍历数组的元素
#include <stdio.h> int main() { int arr[] = { 99, 15, 100, 888, 252 }; int i, j,*p = arr, len = sizeof(arr) / sizeof(int); for(i=0; i<len; i++) { j = *p++; printf("%d ", j ); } printf(" "); return 0; }
1 #include <stdio.h> 2 int main(){ 3 int arr[] = { 99, 15, 100, 888, 252 }; 4 int i, *p = arr, len = sizeof(arr) / sizeof(int); 5 for(i=0; i<len; i++){ 6 printf("%d ", *p++ ); 7 } 8 printf(" "); 9 return 0; 10 }
以上两个输出都是99 15 100 888 252。