I need to declare a variable inside function arguments. Please advice the syntax to use?
I've got something like this:
#include <stdio.h>
int foo (int *a)
{
printf ("%d\n", *a);
}
int main (void)
{
foo (&(int){int a=1});
return 0;
}
And GCC fails with the message:
$ gcc a.c
a.c: In function 'main':
a.c:10: error: expected expression before '{' token
As an option I can put use not named variable like this (same question at russian version of Stack Overflow):
foo(&(int) { 1 });
and it works, but it is interesting why compiler accept {1} but does not accept {int a=1}
{int a=1}, not like just{1}. Sorry if I ask stupid questions.int a = 1; foo(&a);&(int){int a = 1}notation seems to be trying to use it too, it isn't clear what you're hoping for. You can't define a variable name inside the compound literal. It really isn't clear what you're asking for.