例解GNU C之匿名联合或结构体

来源:互联网 发布:做金融网络销售怎么样 编辑:程序博客网 时间:2024/04/29 11:13
 

例解GNU C之匿名联合或结构体

分类: 漫谈C语言 488人阅读 评论(0) 收藏 举报
cstructlinux内核initializationgcc编译器

    前言:计算机语言是编译器和程序员交流的依据和规范,GNU C是GCC特有的功能,在Linux内核中被广泛应用。

    帮助文档:http://gcc.gnu.org/onlinedocs/gcc-4.6.2/gcc/C-Extensions.html#C-Extensions

 

    在GNU C中,可以在结构体中声明某个联合体(或结构体)而不用指出它的名字,如此之后就可以像使用结构体成员一样直接使用其中联合体(或结构体)的成员。

    举例,如test.c: 

[cpp] view plaincopy
  1. #include <stdio.h>  
  2.   
  3. struct test_struct {  
  4.     char *name;  
  5.     union {  
  6.     char gender;  
  7.     int id;  
  8.     };  
  9.     int num;  
  10. };  
  11.   
  12. int main(void)  
  13. {  
  14.     struct test_struct test_struct = {"tanglinux"'F', 28 };  
  15.   
  16.     printf("test_struct.gender = %c, test_struct.id = %d\n",  
  17.         test_struct.gender, test_struct.id);  
  18.   
  19.     return 0;  
  20. }  

    例子输出结果: 

[cpp] view plaincopy
  1. test_struct.gender = F, test_struct.id = 70  

    例子中的第17行,结构体变量test_struct直接使用联合体中的成员gender或id。

    GCC默认情况下可以使用GNU C的各种扩展功能,如果上例使用C99标准编译的话,会产生以下的错误: 

[cpp] view plaincopy
  1. $ gcc -std=c99 test.c -o test  
[cpp] view plaincopy
  1. test.c:8:6: warning: declaration does not declare anything  
  2. test.c: In function 'main':  
  3. test.c:14:12: warning: excess elements in struct initializer  
  4. test.c:14:12: warning: (near initialization for 'test_struct')  
  5. test.c:17:17: error: 'struct test_struct' has no member named 'gender'  
  6. test.c:17:37: error: 'struct test_struct' has no member named 'id'  

    修改上例,使其符合C99标准,如test01.c: 

[cpp] view plaincopy
  1. #include <stdio.h>  
  2.   
  3. union test_union {  
  4.     char gender;  
  5.     int id;  
  6. };  
  7.   
  8. struct test_struct {  
  9.     char *name;  
  10.     union test_union test_union;  
  11.     int num;  
  12. };  
  13.   
  14. int main(void)  
  15. {  
  16.     struct test_struct test_struct = {"tanglinux"'F', 28 };  
  17.   
  18.     printf("test_struct.test_union.gender = %c, test_struct.test_union.id = %d\n",  
  19.         test_struct.test_union.gender, test_struct.test_union.id);  
  20.   
  21.     return 0;  
  22. }  

    例子输出结果: 

[cpp] view plaincopy
  1. $ gcc -std=c99 test01.c -o test01  
[cpp] view plaincopy
  1. test_struct.test_union.gender = F, test_struct.test_union.id = 70  

    在Linux内核中常用匿名联合(或结构体),如在linux-2.6.38.8/fs/sysfs/sysfs.h文件中struct sysfs_dirent结构体的定义: 

[cpp] view plaincopy
  1. struct sysfs_dirent {  
  2.     atomic_t        s_count;  
  3.     atomic_t        s_active;  
  4. #ifdef CONFIG_DEBUG_LOCK_ALLOC  
  5.     struct lockdep_map  dep_map;  
  6. #endif  
  7.     struct sysfs_dirent *s_parent;  
  8.     struct sysfs_dirent *s_sibling;  
  9.     const char      *s_name;  
  10.   
  11.     const void      *s_ns; /* namespace tag */  
  12.     union {  
  13.         struct sysfs_elem_dir       s_dir;  
  14.         struct sysfs_elem_symlink   s_symlink;  
  15.         struct sysfs_elem_attr      s_attr;  
  16.         struct sysfs_elem_bin_attr  s_bin_attr;  
  17.     };  
  18.   
  19.     unsigned int        s_flags;  
  20.     unsigned short      s_mode;  
  21.     ino_t           s_ino;  
  22.     struct sysfs_inode_attrs *s_iattr;  
  23. };  
原创粉丝点击