myStrlen help

I've been taking a course in c this summer, it's a beginners course and we aren't allowed to use any STL (other than stdio). So I printed out the functions from string.h and have been playing around with them.

I know this is a C++ forum, but I think my general idea is okay :)

Here is myStrlen:
1
2
3
4
5
6
size_t myStrlen(const char* str)
{
	const char* pos = str;
	while (*str) ++str;
	return str - pos;
}


It stores the address of str, increases the address of str until the value of str is '\0' and then returns the new address minus the old.

Similarly, functions like strcat and strcpy return the address of the destination string. The only way I know how to return that address is to store it in the beginning and then return it:
1
2
3
4
5
6
7
8
9
10
11
char* myStrcpy(char* dest, const char* source)
{
	char* destPos = dest;
	while (*source)
	{
		*dest = *source;
		++dest; ++source;
	}
	*dest = *source;
	return destPos;
}


What, if anything, can be done about this destPos? I don't like it :). Is there a c/c++ way to do this without declaring a variable?
Last edited on
Topic archived. No new replies allowed.