I would strongly suggest using pointers rather than integer indexes, for fear of integer overflow. Even if size_t
is the same number of bits as char *
, you're adding indices where you wouldn't be adding pointers.
I guess that's more or less academical; if you're calling strcat()
on multi-gigabyte strings you're probably in all sorts of trouble.
Here's a pointer-based version, for completeness' sake:
char *
my_strcat(char *dest, const char *src)
{
char *rdest = dest;
while (*dest)
dest++;
while (*dest++ = *src++)
;
return rdest;
}
Sure, this does take another pointer's worth of space for the rdest
return value, but I think that's a fine trade-off.
Also note that you can't legally define a function called strcat()
in ordinary application code; that whole namespace (public functions with names starting with str
) is reserved for the implementation.