Hacker News new | past | comments | ask | show | jobs | submit login

  foo({.x=2, .y=2})
I remember using this syntax in C in my 2017 project. This is very clear to call methods like that, I used it with minor #defines, i found the inspiration in the book "21st century C."





The post mentions this obvious solution before delving into madness.

i believe this is standard since c99

you do have to cast it, though: `foo((vec2){.x=2, .y=2})`

the oldest MSVC on godbolt also accepts it, so it should be very portable as long as you're not using some embedded compiler from the mid 90s

a related fun thing is that you can pass it by pointer, and the lifetime will extend until the function is done:

`foo(&(vec2){ .x=2, .y=2 })`


Smells like UB. Is this defined in the standard?



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: