pointers-memory · intermediate · ~12 min
Practise pointer assignment with the canonical idiom `while ((*d++ = *s++));`
Write your own strcpy: copy a C string, including its NUL terminator, into a destination buffer.
Implement char *my_strcpy(char *dst, const char *src) that copies src (including the terminating \0) into dst and returns dst. No main — the grader calls it.
dst: a writable buffer large enough to hold src plus its terminator.src: a NUL-terminated source string (may be empty).Returns dst. After the call dst contains a byte-for-byte copy of src including the NUL.
my_strcpy(buf, "hello") -> buf = "hello", returns buf
my_strcpy(buf, "") -> buf = ""
dst.strcpy.Transfer exercise: apply the lesson to a complete function contract.
dst: a writable buffer large enough to hold src plus its terminator.src: a NUL-terminated source string (may be empty).Returns dst. After the call dst contains a byte-for-byte copy of src including the NUL.
Non-NULL live src/dst, terminated src, disjoint regions; dst has strlen(src)+1 writable bytes. Borrow both; return original dst. Do not use library strcpy.
char *my_strcpy(char *dst, const char *src) {
/* TODO */
return dst;
}
Losing the original destination pointer or omitting the terminator.
Empty source still copies its terminator; preserve bytes after the copied string.
O(strlen(src)).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.