3 ms·
could do this instead, right? do { *dst = *src; *dst++; *src++; } while(*dst);
by asddubs 6y ago
could do this instead, right?
do {
*dst = *src;
*dst++;
*src++;
} while(*dst);
- deleted 6y ago[deleted]
- josefx 6y agoI think you are not copying the terminating nul character.
- FartyMcFarter 6y agoUnless the first character was null, in which case it would be ignored by the condition... Also, you don't need to dereference a pointer in order to increase it. The grandparent post's code is just nonsensical.
- asddubs 6y agohaha, I was genuinely asking if it made sense, I don't usually do C. Maybe it helped illustrate that the code is too clever for me, at least. e: I submit my revised code: do { *dst = *src; src++; dst++; } while(*(dst - 1));
- asveikau 6y agoLooking at src[-1] would work but feels like poor form. My advice is stop trying to make do ... while work here, it isn't looking good. I feel like more idiomatic iterating through a string tends to loop on src not being a terminator. while (*src) { *dst = *src; ++src; ++dst; } *dst = '\0'; I feel like this is idiomatic C but needlessly verbose. Most people would combine the increment with the assignment. And most people would recognize putting it in the while condition as a common strcpy.