How is this trivial strncpy implementation

L

lovecreatesbea...

Do I need to repeat inserting '\0' n - strlen(src) times in case
strlen(str) less than n. "C: A reference Manual, 5th" mentions this in
sec 13.3. "K&RC, 2nd" says that insert one '\0' under the same
condition in sec B.3.

Thank you for your time.


/
*******************************************************************************
* copy n characters in string src to dst.

******************************************************************************/

char *strncpy(char *dst, const char *src, size_t n)
{
char *p = dst;

while (n-- && (*dst++ = *src++))
;
return p;
}
 
S

santosh

Do I need to repeat inserting '\0' n - strlen(src) times in case
strlen(str) less than n.

The Standard strncpy does. You can do whatever you want with your
private variations, but do avoid nameclashes with the Standard library,
and give a better more descriptive name. The Standard has a lot of
constraints, not least of which are historical ones, but you can do
better than cryptic six and seven letter names.

snip
 
B

Ben Pfaff

[about strncpy]
Do I need to repeat inserting '\0' n - strlen(src) times in case
strlen(str) less than n.

Yes, strncpy always writes to every byte in the output buffer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top