0

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}

9
  • 2
    What do you mean by "declare it as a variable but not as a constant"? Commented Jan 31, 2018 at 7:52
  • 4
    Why do you need this ? Commented Jan 31, 2018 at 7:52
  • 'as a variable' means something like {int a=1}, not like just {1}. Sorry if I ask stupid questions. Commented Jan 31, 2018 at 7:56
  • 2
    What is the purpose of doing this? I fail to see what advantage this has over int a = 1; foo(&a); Commented Jan 31, 2018 at 7:57
  • 1
    Since you know about the compound literal notation, and your &(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. Commented Jan 31, 2018 at 8:16

1 Answer 1

3

You can use compound literal - I suspect that's what you tried, you almost got it right:

foo (&(int){1});

This is by no means a "constant", I don't know how you got that idea.

Note that a compound literal only have local ("automatic") storage duration - if the calling block goes out of scope, so does the compound literal.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this variant works (as in description). And sorry - I've used wrong terms. You are correct - {1} here is not a "constant" - as I understoot it is just a variable on stack that has no name. My question was how to give a name to this variable. If I try to set {int a=1} instead of just {1} the gcc prints error. I just do not understand how compiler treat this construction and why does it suppose it as an error.
@PeterK. Why on earth do you need to name a variable, when in this case the name is only useful from the point where the variable is created until the function is called? Once inside a function, the variable name is that of the pointer parameter. Your explanation about writing all of this in comments doesn't make sense. This is an XY problem if I ever saw one.
ok, accept this answer (even having it in my initial comment) because it actually resolve my request 'to have declaration inside argument'. Again, I would like to understand why compiler suppose that {int a=1} and just {1} are different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.