THE PROBLEM
-
strncpy (destination, source, max_len)
strncpy is defined to copy at most max_len
characters from source
to destination
, including the trailing null-byte if source
doesn't include a null-byte within the first max_len
bytes.
In your case the trailing null-byte will be including, and with that destination
will be null-terminated directly after "Over"
, which is why you are seeing the described behaviour.
After your call to strncpy
myString
will therefore compare equal to:
"Over\0There"
THE SOLUTION
The most straight-forward solution would be to not copy the trailing null-byte from "Over"
, which is as easy as specifying 4
instead of 5
to strncpy
:
strncpy(myString, "Over", 4);