3 ms·
I know C is an extremely deep language but that doesn’t look like C99 to me. Since when can you set struct member variables piecewise in a…function definition?
by jimbob45 5y ago
I know C is an extremely deep language but that doesn’t look like C99 to me. Since when can you set struct member variables piecewise in a…function definition?
Is this maybe a custom compiler? Or is this actually C++?
- kevin_thibedeau 5y agoIt's a C99 compound literal with designated initializer. C++ doesn't support it.
- nwellnhof 5y agoWhat you're seeing is a compound literal with designated initializers. Both features were introduced in C99.
- rackjack 5y agoIt is C99, which surprised me too. https://stackoverflow.com/a/7487949/11736447 https://stackoverflow.com/a/7487949/11736447
- chisquared 5y agoI encountered code like this in a C99 project recently. I still don't understand it, but I believe it's supposed to be possible.
- MayeulC 5y agoAs other commenters have pointed out, those are designated initializers[1], one of my favorite C99 feature. I's also been picked up recently for inclusion into C++20[2]. However, it seems that C++ adds a specific limitation: > all designators used in the expression must appear in the same order as the data members of T > out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++ Which limits the usefulness, as I've used this syntax to let myself reorder struct fields (and add some) without breaking the API (while obviously breaking the ABI). It's a great tool to use while your API is still in flux. At least an error will be produced, which is much better than just providing values and hoping that the API/ABI remains stable. [1]: https://en.cppreference.com/w/c/language/struct_initialization https://en.cppreference.com/w/c/language/struct_initializati... [2]: https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers https://en.cppreference.com/w/cpp/language/aggregate_initial...