这个 C 函数语法是什么?
What is this C function syntax?
我会说我有中级的 c 编程经验,但是我以前从未见过使用这种语法来创建函数。这让我想起了 JQuery 事件的语法。总的来说,我想要详细解释这是什么以及替代语法可以 be.A link 到我可以阅读更多相关内容的地方也特别好。
// Set handlers to manage the elements inside the Window
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
这是来自 Pebble WatchApp tutorial 的代码片段。
这是一个使用复合文字的函数调用。它等效于以下内容:
WindowHandlers temp = {
.load = main_window_load,
.unload = main_window_unload
};
window_set_window_handlers(s_main_window, temp );
上面还使用了指定的初始化程序,您可以在其中指定要按名称初始化的字段。
假设 WindowHandlers
按顺序仅包含 load
和 unload
,以上等同于:
WindowHandlers temp = { main_window_load, main_window_unload };
window_set_window_handlers(s_main_window, temp );
C standard 更详细地介绍了这些内容。
来自第 6.5.2.5 节:
4 A postfix expression that consists of a parenthesized type
name followed by a brace-enclosed list of initializers is a
compound literal. It provides an unnamed object whose value is
given by the initializer list.
...
9 EXAMPLE 1 The file scope definition
int *p = (int []){2, 4};
initializes p
to point to the first element of an array of
two ints, the first having the value two and the second, four.
The expressions in this compound literal are required to be
constant. The unnamed object has static storage duration.
来自第 6.7.8 节:
1
initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }
initializer-list:
designationopt initializer
initializer-list , designationopt initializer
designation:
designator-list =
designator-list:
designator
designator-list designator
designator:
[ constant-expression ]
.identifier
...
7 If a designator has the form
.identifier
then the current object (defined below) shall have structure
or union type and the identifier shall be the name of a member of
that type.
...
34 EXAMPLE 10 Structure members can be initialized to nonzero values without depending on their order:
div_t answer = { .quot = 2, .rem = -1 };
这是C99以后的标准。它结合了复合文字:
(WindowHandlers) {}
和指定的初始值设定项:
.load = main_window_load,
.unload = main_window_unload
查看 link
What does this dot syntax mean in the Pebble watch development tutorial?
我会说我有中级的 c 编程经验,但是我以前从未见过使用这种语法来创建函数。这让我想起了 JQuery 事件的语法。总的来说,我想要详细解释这是什么以及替代语法可以 be.A link 到我可以阅读更多相关内容的地方也特别好。
// Set handlers to manage the elements inside the Window
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
这是来自 Pebble WatchApp tutorial 的代码片段。
这是一个使用复合文字的函数调用。它等效于以下内容:
WindowHandlers temp = {
.load = main_window_load,
.unload = main_window_unload
};
window_set_window_handlers(s_main_window, temp );
上面还使用了指定的初始化程序,您可以在其中指定要按名称初始化的字段。
假设 WindowHandlers
按顺序仅包含 load
和 unload
,以上等同于:
WindowHandlers temp = { main_window_load, main_window_unload };
window_set_window_handlers(s_main_window, temp );
C standard 更详细地介绍了这些内容。
来自第 6.5.2.5 节:
4 A postfix expression that consists of a parenthesized type name followed by a brace-enclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.
...
9 EXAMPLE 1 The file scope definition
int *p = (int []){2, 4};
initializes
p
to point to the first element of an array of two ints, the first having the value two and the second, four. The expressions in this compound literal are required to be constant. The unnamed object has static storage duration.
来自第 6.7.8 节:
1
initializer: assignment-expression { initializer-list } { initializer-list , } initializer-list: designationopt initializer initializer-list , designationopt initializer designation: designator-list = designator-list: designator designator-list designator designator: [ constant-expression ] .identifier
...
7 If a designator has the form
.identifier
then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type.
...
34 EXAMPLE 10 Structure members can be initialized to nonzero values without depending on their order:
div_t answer = { .quot = 2, .rem = -1 };
这是C99以后的标准。它结合了复合文字:
(WindowHandlers) {}
和指定的初始值设定项:
.load = main_window_load,
.unload = main_window_unload
查看 link What does this dot syntax mean in the Pebble watch development tutorial?