C
chikito.chikito
1. Can someone tell me the difference between these two functions:
void strcpy(char *s1, const char *s2)
{
while(*s1++ = *s2++)
;
}
//function prototype of strcpy follows
char *strcpy(char *s1, const char *s2) // library function
Both functions are doing the same job.
Why the later function has to return a char pointer?
We call both functions in the same manner and we don't sssign the
return pointer to any other pointer. Correct me if I'm wrong.
-------------------------
2. This question is regarding strncpy library function.
//function prototype of strncpy follows
char *strncpy(char *s1, const char *s2, size_t n);
We need to pad '\0' to the end of s1 when n is less than the length of
s2 plus one.
Compare strncpy with strncat. The function strncat pad '\0' by itself.
Why this was left to the programmer in the case of strncpy function.
void strcpy(char *s1, const char *s2)
{
while(*s1++ = *s2++)
;
}
//function prototype of strcpy follows
char *strcpy(char *s1, const char *s2) // library function
Both functions are doing the same job.
Why the later function has to return a char pointer?
We call both functions in the same manner and we don't sssign the
return pointer to any other pointer. Correct me if I'm wrong.
-------------------------
2. This question is regarding strncpy library function.
//function prototype of strncpy follows
char *strncpy(char *s1, const char *s2, size_t n);
We need to pad '\0' to the end of s1 when n is less than the length of
s2 plus one.
Compare strncpy with strncat. The function strncat pad '\0' by itself.
Why this was left to the programmer in the case of strncpy function.