Tag: compound literals

C中的隐秘结构定义

我遇到了以下迷宫定义代码: typedef struct mazeNode { int hasCheese; int tag; struct mazeNode *left; struct mazeNode *right; } maze_t; maze_t maze = { .tag = 1, .left = &(maze_t) { .left = &(maze_t) { .left = &(maze_t) {}, .right = &(maze_t) {} }, .right = &(maze_t) { .right = &(maze_t) {} } }, .right = &(maze_t) { […]

作为参数传递的复合文字的生命周期是多少?

这使用clang编译时没有警告。 typedef struct { int option; int value; } someType; someType *init(someType *ptr) { *ptr = (someType) { .option = ptr->option | ANOTHEROPT, .value = 1 }; return ptr; } int main() { someType *typePtr = init( &(someType) { .option = SOMEOPT }); // do something else with typePtr } 这甚至是有效的C吗? 如果是这样的话:复合文字的生命周期是多少?