C 语言 具有从属关系的结构体分别在不同头文件相互引用的问题

目录

一、说明

二、例子

 

一、说明

给定头文件 father.h, son.h, daughter.h, son.h 和 daughter.h 里有各自的结构体,

而 father.h 里有结构体囊括了 daughter.h 和 son.h 的结构体,

也就是说 father 与 son 和 daughter 有了从属关系,

即father.h 文件可以 include daughter.h 和 son.h, 

而 son.h 和 daughter.h 不可以 include father.h 。

没有从属关系的结构体不可以相互 include。

二、例子

①当 father.h 囊括 daughter.h 和 son.h,结果显示没有异常

father.h

  1 #ifndef __FATHER_H__                                                                               
  2 #define __FATHER_H_
  3 
  4 #include "daughter.h"
  5 #include "son.h"
  6 
  7 typedef struct father_
  8 {
  9     int age;
 10     float height;
 11     float weight;
 12 
 13     son_t son;
 14     daughter_t daughter;
 15 
 16 }father_t;
 17 
 18 #endif

son.h

  1 #ifndef __SON_H__                                                                                  
  2 #define __SON_H_
  3 
  4 typedef struct son_
  5 {
  6     int age;
  7     float height;
  8     float weight;
  9 }son_t;
 10 
 11 #endif

daughter.h

  1 #ifndef __DATGHTER_H__                                                                             
  2 #define __DATGHTER_H_
  3 
  4 typedef struct daughter_
  5 {
  6     int age;
  7     float height;
  8     float weight;
  9 }daughter_t;
 10 
 11 #endif

exam.c

  1 #include                                                                                  
  2 #include "father.h"
  3 #include 
  4 int main(int argc, char** argv)
  5 {
  6     father_t father;
  7     memset(&father, 0, sizeof(father_t));
  8     return 0;
  9 }

编译运行,结果显示并没有异常

# gcc exam.c 
# ./a.out 

② 当 daughter.h 囊括 father.h

  1 #ifndef __DATGHTER_H__                                                                             
  2 #define __DATGHTER_H_
  3 
  4 #include "father.h"
  5 
  6 typedef struct daughter_
  7 {
  8     int age;
  9     float height;
 10     float weight;
 11     father_t father_s;
 12 }daughter_t;
 13 
 14 #endif

编译运行,出错

# gcc exam.c

son.h:4:16: error: redefinition of ‘struct son_’
 typedef struct son_

daughter.h:12:2: note: previous declaration of ‘daughter_t’ was here
 }daughter_t;

son.h:4:16: note: originally defined here
 typedef struct son_

...

③ daughter.h 和 son.h 相互引用

daughter.h

  1 #ifndef __DATGHTER_H__                                                                             
  2 #define __DATGHTER_H_
  3 
  4 #include "son.h"
  5 typedef struct daughter_
  6 {
  7     int age;
  8     float height;
  9     float weight;
 10 }daughter_t;
 11 
 12 #endif

son.h

  1 #ifndef __SON_H__                                                                                  
  2 #define __SON_H_
  3 #include "daughter.h"
  4 typedef struct son_
  5 {
  6     int age;
  7     float height;
  8     float weight;
  9 }son_t;
 10 
 11 #endif

编译运行,出错

# gcc exam.c

daughter.h:5:16: error: redefinition of ‘struct daughter_’
 typedef struct daughter_

son.h:9:2: note: previous declaration of ‘son_t’ was here
 }son_t;

son.h:9:2: error: conflicting types for ‘son_t’
 }son_t;

...

结论:同 说明

 

 

 

你可能感兴趣的:(C语言)