C函数和结构在运行中

Cor*_*sif 5 c struct function on-the-fly

我想知道是否有可能像在c ++中那样在函数调用中初始化struct:

struct point {
  int x;
  int y;
};

some_function(new point(x,y));
Run Code Online (Sandbox Code Playgroud)

谢谢 :)

hac*_*cks 8

是.您可以使用C99中引入的复合文字.

some_function((struct pint) {5, 10});
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,复合文字是左值,这意味着也可以做`some_function(&(struct pint){5,10})`.在某种意义上,它更接近原始请求,它产生一个指针参数(当然,它不会产生动态生命周期). (4认同)
  • @mafso因为`struct`无论如何都是按值传递的(这个问题是关于C而不是C++),关于它的生命周期的论证是静音的. (2认同)