Hi,
beginner in ANSI 'c' .
please can anyone explain me the difference between the strcpy and strncpy ?
Does the strcpy terminates with null char after the copy operation of the strings.?
-kanaks
Hi,
beginner in ANSI 'c' .
please can anyone explain me the difference between the strcpy and strncpy ?
Does the strcpy terminates with null char after the copy operation of the strings.?
-kanaks
Strcpy copies the string from the source to the destination. It takes no precaution to not write outside the end of the destination. It also places a null char in the destination string after copying the string.
Strncpy works the same as strcpy but copies a maximum of n characters (as passed by the argument). If the string is longer than n characters, then strncpy copies n characters and does not insert a null char. If the length of the string is less than n characters, then strncpy behaves exactly like strcpy.
Actually the rest of your string buffer will be padded with '\0''s. Be mindful of the fact that if you are needing to deal with large string buffers with small strings within them, this can exhaust memory resources on the box.
And http://c-faq.com/lib/strncpy.html, which is not exactly like strcpy.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Well to really know the answer, code testing will need to be worked out. From the specification:
"If the array pointed to by s2 is a string that is shorter than n bytes, null bytes are appended to the copy in the array pointed to by s1, until n bytes in all are written."
http://www.opengroup.org/pubs/online...h/strncpy.html
Nice...implementation-defined I guess. Wonder what implementations do not comply with the spec?
Thanks for setting me straight!