在C99中获取rvalue的地址

jus*_*der 9 c gcc c99 undefined-behavior

以下代码编译和工作:

#include <stdio.h>

void print(void* x)
{
    printf("%d", *(int*)x);
}

int main()
{
    print(&((struct { int x, y; }){ .x = 1, .y = 2 })); //outputs 1
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么编译器允许我获取rvalue的地址?这是定义的行为吗?

http://ideone.com/iMwNVr

Yu *_*Hao 11

(struct { int x, y; }){ .x = 1, .y = 2 } 是复合文字,并且:

C99§6.5.2.5复合文字

如果类型名称指定未知大小的数组,则大小由§6.7.8中指定的初始化程序列表确定,复合文字的类型是已完成数组类型的类型.否则(当类型名称指定对象类型时),复合文字的类型是由类型名称指定的类型.在任何一种情况下,结果都是左值.