我在一些源代码中注意到这一行:
if(pthread_create((pthread_t[]){}, 0, start_thread, pthread_args)) {
...
Run Code Online (Sandbox Code Playgroud)
它工作正常,但如何理解第一个参数?似乎,花括号转换为pthread_t[]类型.
我用谷歌搜索,但没有找到答案,只有一些猜测(某些形式的初始化,或c的遗留功能?)
ex *_*ilo 19
这是一个复合文字,由于初始化程序括号不能为空,因此违反约束:
(pthread_t[]){}
Run Code Online (Sandbox Code Playgroud)
使用gcc -std=c99 -Wall -Wextra -Wpedantic它会产生警告:
compound_literal_pthread.c:6:36: warning: ISO C forbids empty initializer braces [-Wpedantic]
pthread_t *ptr = (pthread_t []){};
Run Code Online (Sandbox Code Playgroud)
结果似乎是指向pthread_t,但我没有看到gcc手册中记录的这种行为.请注意,在C++中允许使用空括号作为初始化程序,它们等效于{ 0 }.这种行为似乎是由C支持的,但是没有记录,由gcc支持.我怀疑这就是这里发生的事情,使上面的表达式相当于:
(pthread_t[]){ 0 }
Run Code Online (Sandbox Code Playgroud)
在我的系统上,pthread_t是一个typedeffor unsigned long,所以这个表达式会创建一个pthread_t只包含一个0元素的数组.该数组将衰减为pthread_t函数调用中的指针.