创建struct参数

use*_*405 0 c struct

我有一个结构定义为:

typedef struct pt {
  int x; 
  int y;   
}point;
Run Code Online (Sandbox Code Playgroud)

我还有一个堆栈推送函数声明为:

void push(point p);
Run Code Online (Sandbox Code Playgroud)

现在每当我想调用此函数时,我都可以执行以下操作:

point p = {x_value, y_value};
push(p);
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一个不那么麻烦的解决方法.能够让我在一行中做到这一点的东西.也许是这样的:

push((point){x_value, y_value});
Run Code Online (Sandbox Code Playgroud)

Til*_*gel 6

定义"构造函数"函数:

point make_point(int x, int y)
{
    point result = {x, y};
    return result;
}
Run Code Online (Sandbox Code Playgroud)

然后

push(make_point(x, y));
Run Code Online (Sandbox Code Playgroud)