c - gcc 将临时变量定义为函数参数

标签 c function variables gcc

有没有办法在调用函数时定义一个临时变量用作函数参数?有一个函数:

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/

相关文章:

c++ - 如何在 C 和 C++ 中迭代字符串数组?

function - PowerShell 闭包中的捕获函数

sql - mysql 变量赋值

c - 谐波平均产生不正确的结果

c - C语言中如何删除字符串中的特殊字符

r - 无法将参数传递给自己的函数中的函数

javascript - 如何在本地主机服务器上使用 javascript ajax 调用 java 类函数

variables - 在 PowerShell 中创建动态命名变量

mysql - 在插入更新请求中使用 var 不起作用

c - OpenGL VBO 仅在渲染时上传到 GPU