2 ms·
Arrays in C are often passed around as pointers to the first element, in which case you can no longer get a correct answer with this pattern. This is the most c
by exitb 2y ago
Arrays in C are often passed around as pointers to the first element, in which case you can no longer get a correct answer with this pattern. This is the most common scenario where it stops to apply.
Sometimes the sized array form is actually never available, as with dynamically allocated memory. The correct pattern for all those cases is to pass around the size from where it's known, along with the pointer. One can also use a sentinel element to solve some problems without knowing the size explicitly.
- billforsternz 2y agoYes I know. I've been coding in C for 40 years and C++ for 30 years. It's a simple technique that applies in particular, rather simple, situations with both languages. Edit: The real point is, what was it about his answer that was hated/downvoted? Surely not the fact the sizeof(a)/sizeof(a[o]) requires a little knowledge and care?
- olvy0 2y agoI don't get it either. From sibling comments here I think that it's the fact the bulk of his answer was about C++ while the question was about C. But he noted that in his answer and separated the cases, so I don't see what was all the fuss about. It's certainly not a really bad answer, like that Lundin guy says in the comments. Must be some sort of SO culture we're missing. Because in normal day-to-day conversation between reasonable adults, this would have been an acceptable answer.