Chinaunix首页 | 论坛 | 博客
  • 博客访问: 586467
  • 博文数量: 201
  • 博客积分: 3076
  • 博客等级: 中校
  • 技术积分: 2333
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 19:44
文章分类

全部博文(201)

文章存档

2010年(118)

2009年(83)

我的朋友

分类: C/C++

2009-10-23 16:52:42

原文:http://blog.chinaunix.net/u2/74524/showart_1138735.html

好多次看书、编程时又看到了对结构体这种定义的方法,如:
struct file_operations scull_fops = {
.owner = THIS_MODULE,
.llseek = scull_llseek,
.read = scull_read,
.write = scull_write,
.ioctl = scull_ioctl,
.open = scull_open,
.release = scull_release,
};
前边一直没去真正查找倒底是怎么回事,当时只是想或许是GNU C的扩展语法,因为才谭浩强的书上也没有提到过,看过了一些C语言书也没有提到过。今天一查,原来这个是C99标准,这个目前也是最新的标准,之前我也清楚这件事,但是没意识到这是一个差别。网上找到一篇文章,写得很详细,copy过来了。
原文地址:

在阅读GNU/Linux内核代码时,我 们会遇到一种特殊的结构初始化方式。该方式是某些C教材(如谭二版、K&R二版)

中没有介绍过的。这种方式称为指定初始化(designated initializer)。下面我们看一个例子,

Linux-2.6.x/drivers/usb/storage/usb.c中有这样一个结构体初始化 项目

static struct usb_driver usb_storage_driver = {     

.owner = THIS_MODULE,       

.name = "usb-storage",       

.probe = storage_probe,       

.disconnect = storage_disconnect,       

.id_table = storage_usb_ids,

};    

乍一看,这与我们之前学过的结构体初始化差距甚远。其实这就是前面所说的指定初始化在Linux设备驱动

程序中的一个应用,它源自ISO C99标准。以下我摘录了C Primer Plus第五版中相关章节的内容,

从而就可以很好的理解2.6版内核采用这种方式的优势就在于由此初始化不必严格按照定义时的顺序

这带来了极大的灵活性,其更大的益处还有待大家在开发中结合自身的应用慢慢体会。    

已知一个结构,定义如下

struct book {    

char title[MAXTITL];    

char author[MAXAUTL];    

float value;

};    

C99支持结构的指定初始化项目,其语法与数组的指定初始化项目近似。只是,结构的指定初始化项目使用

点运算符和成员名(而不是方括号和索引值)来标识具体的元素。例如,只初始化book结构的成员value,

可以这样做:     struct book surprise = { .value = 10.99 };    

可以按照任意的顺序使用指定初始化项目:   

struct book gift = {

.value = 25.99,                                    

.author = "James Broadfool",     

       .title = "Rue for the Toad"

};    

正像数组一样,跟在一个指定初始化项目之后的常规初始化项目为跟在指定成员后的成员提供了初始值。

另外,对特定成员的最后一次赋值是它实际获得的值。例如,考虑下列声明:   

struct book gift = {

.value = 18.90,                                     

.author = "Philionna pestle",                 

        0.25

};    

这将把值0.25赋给成员value,因为它在结构声明中紧跟在author成员之后。新的值0.25代替了早先的

赋值18.90。有关designated initializer的进一步信息可以参考c99标准的6.7.8节Ininialization。

标准C89需要初始化语句的元素以固定的顺序出现,和被初始化的数组或结构体中的元素顺序一样。
在ISO C99中,你可以按任何顺序给出这些元素,指明它们对应的数组的下标或结构体的成员名,并且GNU C也把这作为C89模式下的一个扩展。这个扩展没有在GNU C++中实现。
为了指定一个数组下标,在元素值的前面写上“[index] =”。比如:
 int a[6] = { [4] = 29, [2] = 15 };
相当于:
 int a[6] = { 0, 0, 15, 0, 29, 0 };
下标值必须是常量表达式,即使被初始化的数组是自动的。
一个可替代这的语法是在元素值前面写上“.[index]”,没有“=”,但从GCC 2.5开始就不再被使用,但GCC仍然接受。 为了把一系列的元素初始化为相同的值,写为“[first ... last] = value”。这是一个GNU扩展。比如:
 int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
如果其中的值有副作用,这个副作用将只发生一次,而不是范围内的每次初始化一次。
注意,数组的长度是指定的最大值加一。
在结构体的初始化语句中,在元素值的前面用“.fieldname = ”指定要初始化的成员名。例如,给定下面的结构体,
 struct point { int x, y; };
和下面的初始化,
 struct point p = { .y = yvalue, .x = xvalue };
等价于:
 struct point p = { xvalue, yvalue };
另一有相同含义的语法是“.fieldname:”,不过从GCC 2.5开始废除了,就像这里所示:
 struct point p = { y: yvalue, x: xvalue };
[index]”或“.fieldname”就是指示符。在初始化共同体时,你也可以使用一个指示符(或不再使用的冒号语法),来指定共同体的哪个元素应该使用。比如:
 union foo { int i; double d; }; union foo f = { .d = 4 };
将会使用第二个元素把4转换成一个double类型来在共同体存放。相反,把4转换成union foo类型将会把它作为整数i存入共同体,既然它是一个整数。(参考5.24节向共同体类型转换。)
你可以把这种命名元素的技术和连续元素的普通C初始化结合起来。每个没有指示符的初始化元素应用于数组或结构体中的下一个连续的元素。比如,
 int a[6] = { [1] = v1, v2, [4] = v4 };
等价于
 int a[6] = { 0, v1, v2, 0, v4, 0 };
当下标是字符或者属于enum类型时,标识数组初始化语句的元素特别有用。例如:
int whitespace[256] = { [' '] = 1, ['\t'] = 1, ['\h'] = 1, ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
你也可以在“=”前面写上一系列的“.fieldname”和“[index]”指示符来指定一个要初始化的嵌套的子对象;这个列表是相对于和最近的花括号对一致的子对象。比如,用上面的struct point声明:
 struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
如果同一个成员被初始化多次,它将从最后一次初始化中取值。如果任何这样的覆盖初始化有副作用,副作用发生与否是非指定的。目前,gcc会舍弃它们并产生一个警告。


-------------------------------
http://hi.baidu.com/phps/blog/item/4de94efbe9595660024f56fb.html
妙用0元素数组 实现大小可变结构体

#include
#include
#include
struct aa{
    int a;
    int b;
};

struct bb{
    struct aa test[0];
};

int main(void)
{
    struct bb *p=(struct bb*)malloc(sizeof(struct bb)+sizeof(struct aa)*100);
    p->test[0].a=10;
    p->test[0].b=20;
    printf("%d,%d\n",p->test[0].a,p->test[0].b);
    return 0;
}

看这个结构体的定义:
typedef struct st_type
{
     int nCnt;
     int item[0];
}type_a;
(有些编译器会报错无法编译可以改成:)
typedef struct st_type
{
     int nCnt;
     int item[];
}type_a;
    这样我们就可以定义一个可变长的结构,用sizeof(type_a)得到的只有4,就是sizeof(nCnt)=sizeof(int)那个0个元素 的数组没有占用空间,而后我们可以进行变长操作了。
        C语言版:        type_a *p = (type_a*)malloc(sizeof(type_a)+100*sizeof(int));
        C++语言版:        type_a *p = (type_a*)new char[sizeof(type_a)+100*sizeof(int)];
    这样我们就产生了一个长为100的type_a类型的东西用p->item[n]就能简单地访问可变长元素,原理十分简单,分配了比 sizeof(type_a)多的内存后int item[];就有了其意义了,它指向的是int nCnt;后面的内容,是没有内存需要的,而在分配时多分配的内存就可以由其来操控,是个十分好用的技巧。
而释放同样简单:
        C语言版:free(p);
        C++语言版:delete []p;
    这个被称为灵活/弹性数组成员(fleible array member)C89不支持这种东西,C99把它作为一种特例加入了标准。但是,C99所支持的是incomplete type,而不是zero array,形同int item[0];这种形式是非法的,C99支持的形式是形同int item[];只不过有些编译器把int item[0];作为非标准扩展来支持,而且在C99发布之前已经有了这种非标准扩展了,C99发布之后,有些编译器把两者合而为一。


    下面是C99中的相关内容:
6.7.2.1 Structure and union specifiers
    As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. With two exceptions, the flexible array member is ignored. First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.106) Second, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

注意区分 C99新增的“可变长数组”:
C89 标准规定,数组大小必须是在编译时刻确定的;在C99 中,这个标准项被扩展,可以是运行时刻确定的值。也就是说, 可变长数组和 C++ 本身没有关系,只要是支持 C99 的就可以使用可变长数组,包括支持 C99 的 C 编译器。

需要注意的是,可变长数组的维数在数组生存期内是不变的,也就是说,可变长数组不是动态的,可变的只是数组的大小。

引进这一特性的目的是为了支持数值处理。


阅读(643) | 评论(0) | 转发(0) |
0

上一篇:likely && unlikely

下一篇:ioctl

给主人留下些什么吧!~~