3 ms·
Even as someone who doesn't mind writing in C, I would absolutely flag that function as way, way, way, way too terse for no good reason. The code gets ridiculo
by bsder 1y ago
Even as someone who doesn't mind writing in C, I would absolutely flag that function as way, way, way, way too terse for no good reason.
The code gets ridiculously easier to read if you write src[0] and src[1] instead of (*src) and (*(src+1)). And, as a bonus, the precedence problem disappears.
I really don't understand people why write C code like the original code. It's just asking for a bug.
- shash 1y agoIn general avoid frivolous use of pointer arithmetic. foo[k] and *(foo+k) will usually generate identical asm, and the former is just easier to read…
- danhau 1y agoUsually? I‘m willing to bet it will always. I wouldn‘t be surprised if the standard even specifies these two to be identical.
- 1718627440 1y agoa[b] is defined as syntactic sugar for *(a+b), so yes.
- cozzyd 1y agoAnd if you want to go for eclectic, you can do [k]foo
- deleted 1y ago[deleted]
- spyrja 1y agoAnd for the love of God, please don't do 0[src].
- 1718627440 1y agoWhy?
- deleted 1y ago[deleted]
- stevage 1y agoTo be fair, OP rewrote it as `src[1]`. It sounds like this was old code, maybe they weren't a good coder when they started.
- commandersaki 1y agoI know the precedence because I memorise the terse implementation of strcpy, but (also) write *src++ as *(src++) and even though it remains a bit of a mouthful the parenthesis ensures there is no doubt how to evaluate the expression.
- hyghjiyhu 1y agoFor me it would be natural to use src[1] for an array but *(src+1) for an iterator pointer.