我需要在函数参数中声明一个变量。请建议使用的语法?
我得到了类似这样的东西:
#include <stdio.h>
int foo (int *a)
{
printf ("%d\n", *a);
}
int main (void)
{
foo (&(int){int a=1});
return 0;
}而GCC失败了,他的消息是:
$ gcc a.c
a.c: In function 'main':
a.c:10: error: expected expression before '{' token作为一个选项,我可以像这样使用未命名的变量(same question at russian version of Stack Overflow):
foo(&(int) { 1 });它可以工作,但有趣的是为什么编译器接受{1}而不接受{int a=1}
发布于 2018-01-31 20:53:16
你可以使用复合文字--我想这就是你尝试过的,你几乎做对了:
foo (&(int){1});这绝对不是一个“常量”,我不知道你是怎么想到的。
请注意,复合文字只有本地(“自动”)存储持续时间-如果调用块超出作用域,则复合文字也会超出范围。
https://stackoverflow.com/questions/48537078
复制相似问题