How To strcpy()
I’ve written a lot about
strcpy() already, the tiny but famous C library function for copying strings. There is a lot to say about its (in)security, and ways to improve on that. I didn’t realize before that implementing your own
strcpy() function could be part of a job interview for a programmer position. If I were asked to rewrite
strcpy(), I would at least put in an
abort() (for lack of exceptions) when passing in a
NULL pointer. Moreover, I would add a length parameter for doing bounds checking—albeit that might be considered cheating, because it would change the function declaration; the signature of the function.
Rather than zooming in on this rather old topic, I’m going to be lazy for once and just link to this excellent post that tells the story of the solutions given by job applicants for the problem of writing your own
strcpy() function. It’s a must read, hilarious, but also somewhat alerting.
How Do I Copy Thee? Let Me Count the Ways by David Avraamides