越简单越容易出错,严重考验我们在做题的时候是否认真仔细。
引以为戒吧。
1. 函数和宏的问题
#include <stdio.h> #define ADD(p) {p++; (*p)++;} void Add(int *p) {p++; (*p)++;} int main() { int a[] = {0,1,2}; int *pa = a; ADD(pa); ADD(pa); printf("%d,%d,%d/n",a[0],a[1],a[2]); pa = a; Add(pa); Add(pa); printf("%d,%d,%d/n",a[0],a[1],a[2]); return 0; }
输出: 0,2,3
0,4,3
2. 结构体和指针的问题
#include <stdio.h> struct node{ node* next; node* prev; }; int main() { node n[2]; node* p1 = &n[0]; node* p2 = p1++; printf("%d,",p1 - p2); printf("%d,", (unsigned long)&(p1->next) - (unsigned long)&(p2->next)); printf("%d,", (unsigned long)&(p1->prev) - (unsigned long)&(p2->next)); return 0; }
输出: 1,8,12