开发者

I found some bizarre code in the linux kernel, could some one explain it to me?

I found this in initramfs.c, I haven't seen this syntax before, could some one explain what it is doing?

static __initdata int (*actions[])(void) = {
  [Start]   = do_start,
  [Collect] = do_collect,
  [GotHeader] = do_header,
  [SkipIt开发者_运维知识库]  = do_skip,
  [GotName] = do_name,
  [CopyFile]  = do_copy,
  [GotSymlink]  = do_symlink,
  [Reset]   = do_reset,
};

Source Code (line 366): initramfs.c


This is an out-of-sequence array initialization by index. It's like writing

actions[Start] = do_start;
actions[Collect] = do_collect;

except that you can do it as a static initializer.


This is a feature from ISO C99 known as designated initializers. It creates an array and initializes specific elements of that array, not necessarily the first N in order. It's equivalent to the following snippet:

static __initdata int (*actions[SOME_SIZE])(void);
actions[Start]   = do_start;
actions[Collect] = do_collect;
actions[GotHeader] = do_header;
actions[SkipIt]  = do_skip;
actions[GotName] = do_name;
actions[CopyFile]  = do_copy;
actions[GotSymlink]  = do_symlink;
actions[Reset]   = do_reset;

Except that the array will only be as large as it needs to (equal in size to one more than the largest index), and it can be initialized statically at global scope -- you can't run the above code at global scope.

This is not a feature of ANSI C89, but GCC provides this feature as an extension even when compiling code as C89.


Designators in an Array Initialization

The bracketed expressions are called designators, and that's syntax to initialize an array or structure by naming the fields or elements rather than just by ordering the initializers in the same sequence as the declaration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜