c語言結構體自引用和互引用原理及示例程序

      結構體的自引用(self reference),就是在結構體內部,包含指向自身類型結構體的指針。

        結構體的相互引用(mutual reference),就是說在多個結構體中,都包含指向其他結構體的指針。

1. 自引用 結構體

1.1 不使用typedef時

錯誤的方式:

  1. struct tag_1{  
  2.     struct tag_1 A;   /* 結構體 */  
  3.     int value;  
  4. };  

        這種聲明是錯誤的,因爲這種聲明實際上是一個無限循環,成員b是一個結構體,b的內部還會有成員是結構體,依次下去,無線循環。在分配內存的時候,由於無限嵌套,也無法確定這個結構體的長度,所以這種方式是非法的。

正確的方式: (使用指針):

  1. struct tag_1{  
  2.     struct tag_1 *A;  /* 指針 */  
  3.     int value;  
  4. };  

        由於指針的長度是確定的(在32位機器上指針長度爲4),所以編譯器能夠確定該結構體的長度。

1.2 使用typedef 時

錯誤的方式:

  1. typedef struct {  
  2.     int value;  
  3.     NODE *link;  /* 雖然也使用指針,但這裏的問題是:NODE尚未被定義 */  
  4. } NODE;  

這裏的目的是使用typedef爲結構體創建一個別名NODEP。但是這裏是錯誤的,因爲類型名的作用域是從語句的結尾開始,而在結構體內部是不能使用的,因爲還沒定義。

正確的方式:有三種,差別不大,使用哪種都可以。

  1. /*  方法一  */  
  2. typedef struct tag_1{  
  3.     int value;  
  4.     struct tag_1 *link;    
  5. } NODE;  
  6.   
  7.   
  8. /*  方法二  */  
  9. struct tag_2;  
  10. typedef struct tag_2 NODE;  
  11. struct tag_2{  
  12.     int value;  
  13.     NODE *link;      
  14. };  
  15.   
  16.   
  17. /*  方法三  */  
  18. struct tag_3{  
  19.     int value;  
  20.     struct tag *link;    
  21. };  
  22. typedef struct tag_3 NODE;  

2. 相互引用 結構體

錯誤的方式:

  1. typedef struct tag_a{  
  2.     int value;  
  3.     B *bp;  /* 類型B還沒有被定義 */  
  4. } A;  
  5.   
  6. typedef struct tag_b{  
  7.     int value;  
  8.     A *ap;  
  9. } B;   

        錯誤的原因和上面一樣,這裏類型B在定義之 前 就被使用。

正確的方式:(使用“不完全聲明”)

  1. /* 方法一   */   
  2. struct tag_a{  
  3.     struct tag_b *bp;  /* 這裏struct tag_b 還沒有定義,但編譯器可以接受 */  
  4.     int value;  
  5. };  
  6. struct tag_b{  
  7.     struct tag_a *ap;  
  8.     int value;  
  9. };  
  10. typedef struct tag_a A;  
  11. typedef struct tag_b B;   
  12.   
  13.   
  14. /*  方法二   */   
  15. struct tag_a;   /* 使用結構體的不完整聲明(incomplete declaration) */  
  16. struct tag_b;  
  17. typedef struct tag_a A;   
  18. typedef struct tag_b B;  
  19. struct tag_a{  
  20.     struct tag_b *bp;  /* 這裏struct tag_b 還沒有定義,但編譯器可以接受 */  
  21.     int value;  
  22. };  
  23. struct tag_b{  
  24.     struct tag_a *ap;  
  25.     int value;  
  26. };  



/*以下爲自引用程序,VC6.0編譯通過*/

#include <stdio.h>


typedef struct student_tag //student是標識符,stu_type是類型符號,struct student=stu_type
{
    int num;             /*定義成員變量學號*/
    int age;             /*定義成員變量年齡*/
    struct student_tag* stu_point;
}stu_type;






void main()
{
    /*給結構體變量賦值*/
    //struct student stu ={001,"lemon",'F',23,95};
    
stu_type stu ={001,26,&stu};
  
printf("學生信息如下:\n");         /*輸出學生信息*/
    printf("學號:%d\n",stu.num);        /*輸出學號*/
   
   
    printf("年齡:%d\n",stu.age);       /*輸出年齡*/




printf("%p\n",stu.stu_point);    /*指針變量的值,爲四個字節*/



printf("%d\n",sizeof(stu));    /*結構體類型stu_type佔了12個字節*/

    
}


/*下面爲程序運行結果*/



/*以下爲互引用程序,使用結構體的不完整聲明(incomplete declaration)*/



#include <stdio.h>


 struct teacher; //此條語句可省去
 struct student
 {
    int stu_num;           
    struct teacher* tea_point;/* struct teacher省去的話,編譯器也可以接受 */  


};


struct teacher
{
    int tea_num;            
    struct student* stu_point;
};
typedef struct student stu_type;
typedef struct teacher tea_type;




void main()
{
    


   tea_type tea1={000,NULL};
   stu_type stu1={005,NULL};


        stu1.stu_num=001;
             stu1.tea_point=&tea1;


tea1.tea_num=002;
             tea1.stu_point=&stu1;
printf("%d \n",(stu1.tea_point)->tea_num);
printf("%p \n",(stu1.tea_point)->stu_point);




printf("%d \n",(tea1.stu_point)->stu_num);
printf("%p \n",(tea1.stu_point)->tea_point);


}


/*下面爲程序運行結果*/




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章