Re: Using initializer without a cast?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 9 April 2018 at 22:46, Bostjan Mihoric wrote:
> typedef struct MyItemType { double d; } MyItemType;
>
> double Fun1(MyItemType mit)
> {
>         return mit.d;
> }
>
> int main(int argc, char* argv[])
> {
>         // This is ok. Works as expected. If I wanted initializer
>         // to represent a different type, I would cast it.
>         MyItemType mit1 = { 1.0 };
>         MyItemType mit2 = { .d = 1.0 };
>
>         // These require a cast.
>         mit1 = (MyItemType){ 1.0 };
>         mit2 = (MyItemType){ .d = 1.0 };
>         Fun1((MyItemType){ 1.0 });
>         Fun1((MyItemType){ .d = 1.0 });
> }
>
>
> Is there a way of doing initializers in second group without doing a cast?

Only in C++. The C standard defines a compound literal as (type-name){
initializer-list }. If you don't have the type-name then it's not a
compound literal.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux