请解释一下“:”的用法以及结尾的“,”在此结构体初始化 C 代码中
static struct file_operations memory_fops = {
open: memory_open, /* just a selector for the real open */
};
这是来自 uclinux 中的 mem.c 文件
static struct file_operations memory_fops = {
open: memory_open, /* just a selector for the real open */
};
this is from mem.c file in uclinux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 GNU 风格的初始化语法;
open
成员被初始化为memory_open
,其余部分未初始化。 C99 使用不同的语法 (.open = memory_open
)。That's GNU-style initialization syntax; the
open
member is initialized tomemory_open
, the rest is left uninitialized. C99 uses a different syntax (.open = memory_open
).在 C 中,从一开始就允许在大括号括起来的初始值设定项中使用可选的尾随逗号。它在那里,以便您可以在初始化程序中使用统一的逗号放置
,例如,如果出现这种需要,这使得重新排列列表中的初始化程序变得更容易。您是否想使用它是个人喜好的问题。
至于
:
语法,它是 GCC 特定的扩展,正如@geekosaur 已经解释的那样。相应的功能在 C99 中使用不同的语法进行了标准化。In C the optional trailing comma was allowed in brace-enclosed initializers since the beginning of time. It is there so that you can use uniform comma placement in initializers like
This makes it easier, for example, to rearrange the initializers in the list, should such a need arise. Whether you want to use it or not is a matter of personal preference.
As for the
:
syntax, it is a GCC-specific extension as @geekosaur already explained. The corresponding functionality was standardized in C99 with a different syntax.