M
mdh
K & R 5-5 asks for a strncat function ( concat n characters of t);
void mystrncat(char *s, char *t, int n)
{
while ( *s++); /* find end of s */ /* <<<<< 1 */
/* stops at '\0' */ /* <<<<<2 */
while ( *t && n-- > 0)
*s++ = *t++; /* /*<<<<< 3 */
while ( n -- > 0);
*s++ = '\0';
}
Now, with 1 I **Thought** that *s++ fails when *s == '\0', so that s
points to '\0'.
So, when 3 occurs, I thought the first char of t ( *t) is assigned to
the "Old" position of s, which should be '\0', but it is not.
What am I missing.
Thanks in advance.
void mystrncat(char *s, char *t, int n)
{
while ( *s++); /* find end of s */ /* <<<<< 1 */
/* stops at '\0' */ /* <<<<<2 */
while ( *t && n-- > 0)
*s++ = *t++; /* /*<<<<< 3 */
while ( n -- > 0);
*s++ = '\0';
}
Now, with 1 I **Thought** that *s++ fails when *s == '\0', so that s
points to '\0'.
So, when 3 occurs, I thought the first char of t ( *t) is assigned to
the "Old" position of s, which should be '\0', but it is not.
What am I missing.
Thanks in advance.