5 ms·
Oh neat, thanks for posting this. I should note that I usually write articles to kick off discussions on my own forum, and then revise them after all input is
by byuu 15y ago
Oh neat, thanks for posting this.
I should note that I usually write articles to kick off discussions on my own forum, and then revise them after all input is gathered. They almost never appear on other sites, especially pre-revision.
I'm not saying the strmcpy/cat functions there now are the best solution, and I'm open to hearing what's wrong with them so that we can improve upon them.
- dlazar 15y agoOnly commenting on the coding style: that kind of formatting is just looking for trouble. Quick, tell me which of the functions below contains a bug? //return = strlen(target) unsigned strmcat(char *target, const char *source, unsigned length) { const char *origin = target; while(*target && length) target++, length--; return (target - origin) + strmcpy(target, source, length); } //return = strlen(target) unsigned strmcat(char *target, const char *source, unsigned length) { const char *origin = target; while(*target && length) target++; length--; return (target - origin) + strmcpy(target, source, length); } How about this? //return = strlen(target) unsigned strmcat(char *target, const char *source, unsigned length) { const char *origin = target; while(*target && length) target++, length--; return (target - origin) + strmcpy(target, source, length); } //return = strlen(target) unsigned strmcat(char *target, const char *source, unsigned length) { const char *origin = target; while(*target && length) target++; length--; return (target - origin) + strmcpy(target, source, length); } Granted, it's a tiny example, but you're going to be writing fewer bugs in the long run if you avoid being clever. So here are two rules to start with: 1. Never put more than one statement on one line. 2. Never use the comma operator outside the "for" syntax.