在C中是否可以创建"内联"结构?
typedef struct {
int x;
int y;
} Point;
Point f(int x) {
Point retval = { .x = x, .y = x*x };
return retval;
}
Point g(int x) {
return { .x = x, .y = x*x };
}
Run Code Online (Sandbox Code Playgroud)
f是有效的,g不是.同样适用于函数调用:
float distance(Point a, Point b) {
return 0.0;
}
int main() {
distance({0, 0}, {1, 1})
}
Run Code Online (Sandbox Code Playgroud)
是否有可能在不必使用额外的临时变量的情况下创建这些结构(我猜这将被编译器优化,但可读性也很重要)?