Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference between memcpy and strncpy

219 views
Skip to first unread message

pavunkumar

unread,
Jul 11, 2009, 3:24:59 AM7/11/09
to pavu...@gmail.com
Dear Friends

Can you explain the difference between memcpy and
strncpy function
Actually , please classify function by usage

Thanks


Xavier Roche

unread,
Jul 11, 2009, 3:45:18 AM7/11/09
to
pavunkumar a �crit :

> Can you explain the difference between memcpy and
> strncpy function

strncpy copies "at most" n bytes, ending at the first \0 seen in the source.

Personal note: strncpy() is one of the cursed functions in C most people
will (should) never use, as it may lead to unterminated destination.

<http://opengroup.org/onlinepubs/007908775/xsh/strncpy.html>
<http://www.opengroup.org/onlinepubs/009695399/functions/memcpy.html>

Casper H.S. Dik

unread,
Jul 12, 2009, 10:42:24 AM7/12/09
to
Xavier Roche <xro...@free.fr.NOSPAM.invalid> writes:

>pavunkumar a �crit :
>> Can you explain the difference between memcpy and
>> strncpy function

>strncpy copies "at most" n bytes, ending at the first \0 seen in the source.

And zero fills the rest!

It's only useful for copy strings to C structs, specifically when those
structs are then written to a file.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

0 new messages