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

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

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

 

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

    举例,如test.c: 

#include <stdio.h>

struct test_struct {
    char *name;
    union {
	char gender;
	int id;
    };
    int num;
};

int main(void)
{
    struct test_struct test_struct = {"tanglinux", 'F', 28 };

    printf("test_struct.gender = %c, test_struct.id = %d\n",
	    test_struct.gender, test_struct.id);

    return 0;
}

    例子输出结果: 

test_struct.gender = F, test_struct.id = 70

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

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

$ gcc -std=c99 test.c -o test

test.c:8:6: warning: declaration does not declare anything
test.c: In function 'main':
test.c:14:12: warning: excess elements in struct initializer
test.c:14:12: warning: (near initialization for 'test_struct')
test.c:17:17: error: 'struct test_struct' has no member named 'gender'
test.c:17:37: error: 'struct test_struct' has no member named 'id'

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

#include <stdio.h>

union test_union {
    char gender;
    int id;
};

struct test_struct {
    char *name;
    union test_union test_union;
    int num;
};

int main(void)
{
    struct test_struct test_struct = {"tanglinux", 'F', 28 };

    printf("test_struct.test_union.gender = %c, test_struct.test_union.id = %d\n",
	    test_struct.test_union.gender, test_struct.test_union.id);

    return 0;
}

    例子输出结果: 

$ gcc -std=c99 test01.c -o test01

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结构体的定义: 

struct sysfs_dirent {
	atomic_t		s_count;
	atomic_t		s_active;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lockdep_map	dep_map;
#endif
	struct sysfs_dirent	*s_parent;
	struct sysfs_dirent	*s_sibling;
	const char		*s_name;

	const void		*s_ns; /* namespace tag */
	union {
		struct sysfs_elem_dir		s_dir;
		struct sysfs_elem_symlink	s_symlink;
		struct sysfs_elem_attr		s_attr;
		struct sysfs_elem_bin_attr	s_bin_attr;
	};

	unsigned int		s_flags;
	unsigned short		s_mode;
	ino_t			s_ino;
	struct sysfs_inode_attrs *s_iattr;
};

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tanglinux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值