strncpy

David Brown dave at motto.UUCP
Thu Jan 25 01:25:19 AEST 1990


In article <4128 at helios.TAMU.EDU> john at stat.tamu.edu (John S. Price) writes:
>Every manual I read says this:
>"... If the string pointed to by the source has fewer than count
>characters, nulls are added to the end of dest until count characters 
>have been copied." -- C - The Complete Reference.
>
>"... strncpy(s,t,n)    copy exactly n characters; null pad if necessary"
> -- Unix Programming Environment
>

We found this useful when dealing with the keys of ISAM files under VMS.
If the key was a C string, we used strncpy() to copy it rather than
strcpy().  This guaranteed that the bytes following the '\0' were all
zeroes.  This was important because the filesystem compared all bytes of
the key.

If we had used strcpy(), two keys which were the same string value could
be different as far as the filesystem was concerned, because of garbage
following the '\0'.

 -----------------------------------------------------------------------------
|  David C. Brown	    |  uunet!mnetor!motto!dave			      |
|  Motorola Canada, Ltd.    |  416-499-1441 ext 3708			      |
|  Communications Division  |  "Of course I'm above average!  Who isn't?"     |
 -----------------------------------------------------------------------------



More information about the Comp.lang.c mailing list