Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass a struct to a function without creating a variable? [duplicate]

Tags:

c

In C++ this code is valid

struct foo{
    int x;
};

int bar(foo f);

bar({1});

However I get an error when I try to do something similar in C. Is there way a to pass a struct to a function without actually creating a variable for it?

like image 628
zee Avatar asked Jan 27 '26 22:01

zee


1 Answers

You need a compound literal for this:

bar((struct foo){1});
like image 86
dbush Avatar answered Jan 29 '26 13:01

dbush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!