Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between strncpy and strxfrm

Tags:

c

I can't seem to get my head around this, what is the basic difference between these two functions:

char *strncpy(char *str1, const char *str2, size_t  count);

and

size_t strxfrm(char *str1, const char *str2, size_t  count);

To me it seems they both copy count characters from str2 to str1. What I really want to know is, when should I use either of them?

like image 738
audin Avatar asked Dec 13 '25 23:12

audin


1 Answers

The difference is in the return value:

The strcpy() and strncpy() functions return a pointer to the destination string.

Upon successful completion, strxfrm() shall return the length of the transformed string (not including the terminating null byte). If the value returned is n or more, the contents of the array pointed to by s1 are unspecified.

like image 141
Igor Avatar answered Dec 16 '25 20:12

Igor