user-0 提问: 小菜不怕菜之再请教一个c语言指针相关的问题

回复:
user: user-1
content: VC编译选项加/EP /P（项目、属性、配置属性、C/C++、预处理器、预处理到文件：是，预处理取消显示行号：是），重新编译，查看宏展开后对应的.i文件。gcc加-E http://bbs.csdn.net/topics/391003898

user: user-0
content: 引用 4 楼 user-3 的回复:建议使用函数，就逄内联函数也比宏好，方便调试与查看，实在也写成宏也先按函数方式写正确了再改成宏 恩呢，确实是这样。。

user: user-0
content: 引用 5 楼 cfjtaishan 的回复:楼主的代码能编译过去吗？ 指针类型冲突呢，指针会设计到其类型，不能简单做加减，另外楼主的代码在地址做加减时没有考虑到编译器的位数，有的是32位，有的是64位，因此代码即使可以编译通过也不能在两个平台上都能使用（32位，64位） 嗯，确实编译器会报warning,我以为warning都是没关系的， warning是2.c: In function ‘main’: 2.c:34:8: warning: assignment makes integer from pointer without a cast [-Wint-conversion]    a[0] = p1; 改为a[0]=(unsigned int)p1;后报 2.c: In function ‘main’: 2.c:34:10: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]    a[0] = (unsigned int)p1; 后来测试了一下，才知道原来a[0]是4位，p1是8位。 重新定义了一下全局变量。以上告警全部消失了。 #include <stdio.h> #include <stdlib.h> long int a[5]; long int d; #define unlink(P,BK,FD){\ /*FD = P->fd;\ BK = P->bk;\ */\ d=a+2;\ FD=d-0x18;\ BK=d-0x10;\     if (__builtin_expect (FD->bk != P || BK->fd != P, 0))\       printf ("corrupted double-linked list\n");\     else {\         FD->bk = BK;\ /**d(P)=d-0x18; */\         BK->fd = FD;\ }\ }  struct s {   long int b;   long int c;   struct s *fd;   struct s *bk; }; main () {   struct s *FD;   struct s *BK;   struct s *p1 = malloc (sizeof (struct s));   a[0] = (long int)p1;   p1->b = 1;   struct s *p2 = malloc (sizeof (struct s));   a[1] = (long int)p2;   p2->b = 2;   struct s *p3 = malloc (sizeof (struct s));   p3->b = 3;   a[2] = (long int)p3;   struct s *p4 = malloc (sizeof (struct s));   a[3] = (long int)p4;   p4->b = 4;   struct s *p5 = malloc (sizeof (struct s));   a[4] = (long int)p5;   p5->b = 5;   p1->fd = p2;   p2->fd = p3;   p3->fd = p4;   p4->fd = p5;   p5->bk = p4;   p4->bk = p3;   p3->bk = p2;   p2->bk = p1;   struct s *p;   p = p1;   while (p != NULL)     {       printf ("%d->", p->b);       p = p->fd;     }   printf ("\n");   unlink (p3, FD, BK);   p = p1;   while (p != NULL)     {       printf ("%d->", p->b);       p = p->fd;     }   free (p1);   free (p2);   free (p3);   free (p4);   free (p5); }  但是还有如下告警，但是已经达到目的了，不管了，没想到大家回复这么快，多谢多谢--- [test@bogon 4-ReeHY-main]$ gcc -o 2 2.c 2.c:27:1: warning: return type defaults to ‘int’ [-Wimplicit-int]  main ()  ^ 2.c: In function ‘main’: 2.c:8:2: warning: assignment makes integer from pointer without a cast [-Wint-conversion]  d=a+2;\   ^ 2.c:62:3: note: in expansion of macro ‘unlink’    unlink (p3, FD, BK);    ^ 2.c:9:3: warning: assignment makes pointer from integer without a cast [-Wint-conversion]  FD=d-0x18;\    ^ 2.c:62:3: note: in expansion of macro ‘unlink’    unlink (p3, FD, BK);    ^ 2.c:10:3: warning: assignment makes pointer from integer without a cast [-Wint-conversion]  BK=d-0x10;\    ^ 2.c:62:3: note: in expansion of macro ‘unlink’    unlink (p3, FD, BK);    ^

user: user-2
content: #include <stdio.h> #include <stdlib.h>  //unsigned int a[5]; unsigned int d; //以下unlink摘自malloc.c #define unlink(P,BK,FD){\     /*FD = P->fd;\       BK = P->bk;\ */\     d = a[2];\     FD= (struct s *)(d-0x18);\     BK= (struct s *)(d-0x10);\     printf("FD->bk:%p\n",FD->bk);\     printf("p:%p\n",P);\     printf("BK->fd:%p\n",BK->fd);\     if (__builtin_expect (FD->bk != P || BK->fd != P, 0))\     printf ("corrupted double-linked list\n");\     else {\         FD->bk = BK;\         BK->fd = FD;\     }\ }  struct s {     long int b;     long int c;     struct s *fd;     struct s *bk; }; struct s *a[5];  int main () {     struct s *FD;     struct s *BK;     struct s *p1 = (struct s *)malloc (sizeof (struct s));     a[0] = p1;     p1->b = 1;     struct s *p2 = (struct s *)malloc (sizeof (struct s));     a[1] = p2;     p2->b = 2;     struct s *p3 = (struct s *)malloc (sizeof (struct s));     p3->b = 3;     a[2] = p3;     printf ("%p\n", p3);     struct s *p4 = (struct s *)malloc (sizeof (struct s));     a[3] = p4;     p4->b = 4;     struct s *p5 = (struct s *)malloc (sizeof (struct s));     a[4] = p5;     p5->b = 5;     p1->fd = p2;     p2->fd = p3;     p3->fd = p4;     p4->fd = p5;     p5->bk = p4;     p4->bk = p3;     p3->bk = p2;     p2->bk = p1;     struct s *p;     p = p1;     while (p != NULL)//输出一次链表     {         printf ("%ld->", p->b);         p = p->fd;     }     printf ("\n");     unlink (p3, FD, BK);//调用unlink宏，摘p3     p = p1;     while (p != NULL)//摘完之后，再打印链表     {         printf ("%ld->", p->b);         p = p->fd;     }     free (p1);     free (p2);     free (p3);     free (p4);     free (p5); }  d = a+2需要改一下，改成d = a[2]；

user: user-2
content: 楼主的代码能编译过去吗？ 指针类型冲突呢，指针会设计到其类型，不能简单做加减，另外楼主的代码在地址做加减时没有考虑到编译器的位数，有的是32位，有的是64位，因此代码即使可以编译通过也不能在两个平台上都能使用（32位，64位）

user: user-3
content: 建议使用函数，就逄内联函数也比宏好，方便调试与查看，实在也写成宏也先按函数方式写正确了再改成宏

user: user-0
content: 以上代码，在linux下，可以直接用gcc编译通过，多谢

user: user-0
content: 楼上你回复太快了。。 忘了csdn是不允许一个帖子没发完，继续编辑的。下面是把这个问题完整的描述一遍 问题是这样的，我建立了一个简单的链表p1->p2->p3->p4->p5，使用unlink宏去把p3从这个链表里面摘下来。问题出在unlink宏里面，明明运行时打印出来的FD->bk 和=P、BK->fd 和 P都是相等的，为什么每次都落到{“corrupted。。。”}这个判断结果里面，请大大帮忙指点，多谢  #include <stdio.h> #include <stdlib.h> unsigned int a[5];//这个是保存链表中指针的数组 unsigned int d; //以下unlink摘自malloc.c #define unlink(P,BK,FD){\ /*FD = P->fd;\ BK = P->bk;\ */\ d=a+2;\ FD=d-0x18;\ BK=d-0x10;\ printf("FD->bk:%x\n",FD->bk);\ printf("p:%x\n",P);\ printf("BK->fd:%x\n",BK->fd);\     if (__builtin_expect (FD->bk != P || BK->fd != P, 0))\       printf ("corrupted double-linked list\n");\     else {\         FD->bk = BK;\         BK->fd = FD;\ }\ }   struct s {   long int b;   long int c;   struct s *fd;   struct s *bk; }; main () {   struct s *FD;   struct s *BK;   struct s *p1 = malloc (sizeof (struct s));   a[0] = p1;   p1->b = 1;   struct s *p2 = malloc (sizeof (struct s));   a[1] = p2;   p2->b = 2;   struct s *p3 = malloc (sizeof (struct s));   p3->b = 3;   a[2] = p3;   printf ("%x\n", p3);   struct s *p4 = malloc (sizeof (struct s));   a[3] = p4;   p4->b = 4;   struct s *p5 = malloc (sizeof (struct s));   a[4] = p5;   p5->b = 5;   p1->fd = p2;   p2->fd = p3;   p3->fd = p4;   p4->fd = p5;   p5->bk = p4;   p4->bk = p3;   p3->bk = p2;   p2->bk = p1;   struct s *p;   p = p1;   while (p != NULL)//输出一次链表     {       printf ("%d->", p->b);       p = p->fd;     }   printf ("\n");   unlink (p3, FD, BK);//调用unlink宏，摘p3   p = p1;   while (p != NULL)//摘完之后，再打印链表     {       printf ("%d->", p->b);       p = p->fd;     }   free (p1);   free (p2);   free (p3);   free (p4);   free (p5); } 运行结果： [test@localhost 4-ReeHY-main]$ ./2 17a2070 //p3 1->2->3->4->5-> //没尝试摘之前的链表 FD->bk:17a2070 //进入宏以后，可以看到，这里FD->bk是和P相等的 p:17a2070 BK->fd:17a2070  //可以看到，这里BK->fd也是和P相等的 corrupted double-linked list //但是，并没有绕过这个检查，下面输出的还是原来的链表 1->2->3->4->5->[test@localhost 4-ReeHY-main]$ 为什么呢？

user: user-1
content: 仅供参考：//不带表头结点的循环双向链表 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> #include <locale.h> struct NODE {     int          data;     struct NODE *next;     struct NODE *prev; } *head,*p,*q,*s,*p1,*p2,*q1,**ta; int i,k,n,t,m,v,N=10; int main() {     setlocale(LC_ALL,"chs");     srand(time(NULL));      head=NULL;      printf("创建%d个节点的循环双向链表:",N);//创建N个节点的循环双向链表     p=head;     for (i=0;i<N;i++) {         q=(struct NODE *)malloc(sizeof(struct NODE));         if (NULL==q) exit(1);         q->data=rand()%100;//填写0..99的随机值         q->next=NULL;         q->prev=NULL;         if (NULL==p) {             head=q;             p=head;         } else {             p->next=q;             q->prev=p;             p=q;         }     }     p->next=head;     head->prev=p;      //输出整个循环双向链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d(=)",s->data);         if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}         s=s->next;         if (s==head) {             printf(" Loop\n");             break;         }     }      k=3;     v=5;     printf("将值为%d的结点插入到循环双向链表的第%d个结点前:",v,k);//将值为v的结点插入到循环双向链表的第k个结点前     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==1) {             q=(struct NODE *)malloc(sizeof(struct NODE));             if (NULL==q) exit(1);             q->data=v;             q->next=head;             q->prev=head->prev;             head->prev->next=q;             head->prev=q;             head=q;             break;         } else {             if (k-1==n) {                 q=(struct NODE *)malloc(sizeof(struct NODE));                 if (NULL==q) exit(1);                 q->data=v;                 q->next=p->next;                 q->prev=p;                 p->next->prev=q;                 p->next=q;                 break;             }         }         p=p->next;         if (p==head) break;     }      //输出整个循环双向链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d(=)",s->data);         if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}         s=s->next;         if (s==head) {             printf(" Loop\n");             break;         }     }      k=5;     printf("删除第%d个节点:",k);//删除第k个节点     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==1) {             q=head;             head=head->next;             head->prev=q->prev;             q->prev->next=head;             if (q==head) head=NULL;             free(q);             break;         } else {             if (k-1==n) {                 q=p->next;                 p->next=q->next;                 q->next->prev=p;                 free(q);                 break;             }         }         p=p->next;         if (p==head) break;     }      //输出整个循环双向链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d(=)",s->data);         if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}         s=s->next;         if (s==head) {             printf(" Loop\n");             break;         }     }      printf("从小到大排序:");//从小到大排序     for (p=head;p!=NULL;) {         p1=p->prev;         for (q=p->next,q1=p;q!=NULL;) {             if (p->data > q->data) {                  //交换data //              printf("swap %02d %02d\n",p->data,q->data); //              t=p->data;p->data=q->data;q->data=t;                  //或者                  //交换prev和next //              printf("swap %02d %02d\n",p->data,q->data);                 if (p==head) {//p是头                     if (p->next==q) {//pq挨着                         head=q;                         head->prev=p1;                         p1->next=q;                         p->next=q->next;                         p->prev=q;                         q->next->prev=p;                         q->next=p;                         q=p;                         p=head;                     } else {//pq不挨着                         head=q;                         head->prev=p1;                         p1->next=q;                         p2=p->next;                         p->next=q->next;                         p->next->prev=p;                         p->prev=q1;                         q->next->prev=p;                         q->next=p2;                         p2->prev=q;                         q1->next=p;                         q=p;                         p=head;                     }                 } else {//p不是头                     if (p->next==q) {//pq挨着                         p1->next=q;                         p->next=q->next;                         p->prev=q;                         q->next->prev=p;                         q->next=p;                         q->prev=p1;                         q=p;                         p=p1->next;                     } else {//pq不挨着                         p1->next=q;                         p2=p->next;                         p->next=q->next;                         p->prev=q->prev;                         q->next->prev=p;                         q->next=p2;                         q->prev=p1;                         p2->prev=q;                         q1->next=p;                         q=p;                         p=p1->next;                     }                 }                  //输出整个循环双向链表 //              s=head; //              while (1) { //                  if (NULL==s) { //                      printf("\n"); //                      break; //                  } //                  printf("%02d(=)",s->data); //                  if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;} //                  s=s->next; //                  if (s==head) { //                      printf(" Loop\n"); //                      break; //                  } //              } //              printf("head,p,q:%02d %02d %02d\n",head->data,p->data,q->data); //              getchar();             }             q1=q;q=q->next;             if (q==head) break;         }         p=p->next;         if (p->next==head) break;     }      //输出整个循环双向链表并计算链表长度n     n=0;     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d(=)",s->data);         if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}         n++;         s=s->next;         if (s==head) {             printf(" Loop\n");             break;         }     }      printf("将整个链表逆序:");//将整个链表逆序     if (n>=2) {         p=head;         while (1) {             q=p->prev;p->prev=p->next;p->next=q;             p=p->next;             if (p==head) break;         }     }      //输出整个循环双向链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d(=)",s->data);         if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}         s=s->next;         if (s==head) {             printf(" Loop\n");             break;         }     }      m=4;     n=6;     printf("将循环双向链表中前%d个结点和后%d个结点进行互换:",m,n);//将循环双向链表中前m个结点和后n个结点进行互换,m+n为链表总长     k=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         k++;         if (m==k) {             q=p;             break;         }         p=p->next;         if (p==head) break;     }     head=q->next;      //输出整个循环双向链表     s=head;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d(=)",s->data);         if (NULL!=s->next && s->next->prev!=s) {printf(" !\n");break;}         s=s->next;         if (s==head) {             printf(" Loop\n");             break;         }     }      //释放所有节点     p=head;     while (1) {         if (NULL==p) {             break;         }         q=p->next;         free(p);         p=q;         if (p==head) {             break;         }     }      return 0; } //创建10个节点的循环双向链表:12(=)46(=)31(=)63(=)25(=)91(=)39(=)02(=)92(=)68(=) Loop //将值为5的结点插入到循环双向链表的第3个结点前:12(=)46(=)05(=)31(=)63(=)25(=)91(=)39(=)02(=)92(=)68(=) Loop //删除第5个节点:12(=)46(=)05(=)31(=)25(=)91(=)39(=)02(=)92(=)68(=) Loop //从小到大排序:02(=)05(=)12(=)25(=)31(=)39(=)46(=)68(=)91(=)92(=) Loop //将整个链表逆序:92(=)91(=)68(=)46(=)39(=)31(=)25(=)12(=)05(=)02(=) Loop //将循环双向链表中前4个结点和后6个结点进行互换:39(=)31(=)25(=)12(=)05(=)02(=)92(=)91(=)68(=)46(=) Loop //