char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
simply copy's the string in src
to the location pointed to by dest
. Woe betide you if the space pointed to by dest
is not large enough, as this str function
will gladly overwrite any memory past that point, which is why it's probably wiser to use strncpy
, which only copy's the first n
In cases where n is greater than the length of src (which is determined by the location of the standard null terminator), the extra space is padded with nulls. In cases where n is less than the length of src, no null characters will be present.
This is a function where it is critical to allocate enough memory
. You will get screwed over
if you do not have enough space allocated and you do a straight strcpy
. Some checks with strlen
, or simply using strncpy
, would be much safer.
Programming Rule #1: Never Assume User Input Is What You Expect.