My C++ class is going over C-style strings and working with pointers. I'm to write a function that has three parameters: a char * s1, a const char * s2, and a size_t max, which represents that maximum size of the s1 buffer. I am to append the characters in s2 to the end of s1. The directions advise me to make sure there is only one '\0' at the end of the combined characters and I am not to go beyond the end of the buffer I'm asked to copy to. The function will return a pointer to the first character in s1.
I cannot use any functions in the standard library. What I can use are pointers, pointer arithmetic or array notation.
I've started, but not sure where to go.
1 2 3 4 5 6 7 8 9
constchar * myFunction (char * s1, constchar * s2, size_t max)
{
char * begin = s1;
while (*s1) s1++;
while (s1 < begin + max - 1 && *s2 != '\0')
*s1++ = *s2++;
return s1;
}