"Flat Initializer Lists" is given as an example in K&R C I think, at least the first edition, when writing those extra braces to fill out an initializer must have felt very redundant. These days many compilers will warn if you do this, however, as it is rare people do this and usually indicates a misunderstanding of the type used. I think it's quite readable though, so it's a shame it causes warnings. What do you thi…
Mildly interesting quirks of C
41–50 of 91 posts
Re: Mildly interesting quirks of C
#42As someone who's moved on to Rust, I see this as one long list of nightmares.
Fortunately GCC has a whole bucket-list of warnings that can be enabled (I like compiling with -Wall -Wextra -pedantic, myself) which can, combined with proper tooling, catch many issues.
Re: Mildly interesting quirks of C
#43Earlier quoted context omitted.
"Dynamic array" refers to block of memory allocated via malloc() which you just happen to use as array. "Flexible array member" [0] is when you have a struct and its last member is an array with unspecified size. An example: #include #include struct Foo { int len; int* arr; // dynamic "array" }; struct Bar { int len; int arr[]; // FAM }; int main() { const int n = 12; // have to allocate myself; no guarante it will b…
>"Dynamic array" refers to block of memory allocated via malloc() which you just happen to use as array. No. A dynamic array is an array which can be expanded or shrinked during its runtime life. The fact that C/C++ uses malloc for that (and btw, it's not the only way to do it) it's her problem. In other languages you have dynamic arrays that can be expanded/shrinked without using an extra line - main reason why nowa…
Re: Mildly interesting quirks of C
#44What? UB is clearly undesirable, but assuming it is impossible and deducing other outcomes must be meant are clearly wrong assumptions by the compiler writer.
More sensible compilers (including older version of clang) do the right thing (TM) here and yield a compiler error.
There were earlier attempts at do-what-i-mean programming languages. They are rightfully buried in history.
Re: Mildly interesting quirks of C
#45Whenever the subject of C/C++ quirks is brought up, I always like to point out the Deep C/C++ presentation: http://www.pvv.org/~oma/DeepC_slides_oct2011.pdf Source: https://freecomputerbooks.com/Deep-C-and-Cpp.html#downloadLi... Previous discussion: https://news.ycombinator.com/item?id=3093323 It could be considered a bit dated at this point (It's before C++11) but I find it still both entertaining and educating.
Re: Mildly interesting quirks of C
#46My favorite C "quirk": If you have an array and you want to access an item of it, you can swap the variable and the index number (put the variable name inside brackets and the number outside): a[5] is the same as: 5[a] why? a[5] is actually sugar for *(a + 5), so by commutative property, you can also do *(5 + a) to access the same memory position :-)
That's #15 on the list.
Re: Mildly interesting quirks of C
#47After learning about a few of these I started to understand why people coming from C always said that PHP is a well designed language… But OK, I understand that my mind is just not made for the complexity of C. Most likely I'm not a real programmer. I get instantly knots in my brain and start to bang my head against the wall when I need to look for too long on C code. Actually even C documentation is enough to trigge…
Re: Mildly interesting quirks of C
#48> UB is impossible What? UB is clearly undesirable , but assuming it is impossible and deducing other outcomes must be meant are clearly wrong assumptions by the compiler writer. More sensible compilers (including older version of clang) do the right thing (TM) here and yield a compiler error. There were earlier attempts at do-what-i-mean programming languages. They are rightfully buried in history.
It's debatable whether it's a good assumption. But not wrong.
Re: Mildly interesting quirks of C
#49Re: Mildly interesting quirks of C
#50The top comment in the gist looks like from "Hacker News Parody Thread".