有没有办法在调用函数时定义一个临时变量用作函数参数?有一个函数:
int hello(const int *p);
调用它
int a = 10;
hello(&a);
但是 var a
在那之后不会被使用,所以我希望它们在一行中,像这样:
hello(&(int a = 10)); // can't compile
hello(&int(10)); // can't compile
表达这个的正确形式是什么?
最佳答案
回答
复合文字的正确语法如下:
hello(&(const int){10});
复合文字
根据 IBM :
The syntax for a compound literal resembles that of a cast expression. However, a compound literal is an lvalue, while the result of a cast expression is not. Furthermore, a cast can only convert to scalar types or void, whereas a compound literal results in an object of the specified type.
关于c - gcc 将临时变量定义为函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25111135/