Copy A String
You can code your own copy string function using the following way:
char *my_strcpy(char *dest, const char *src) {
char *p = dest;
while (*src != '\0') {
*p++ = *src++;
}
*p = '\0';
return dest;
}
In here, the line *p++ = *src++
will copy value of src
to p
and then increment both.
In the end where src == nul
we stopped copying so we manually set *p = nul
in the end.
In here const keyword will make the compiler throw if we're trying to modify src
.