Originally Posted by
Adak
Other than programming guys and gals, people don't like using pointers. Their blood pressure rises dangerously just mentioning the term. Use indices.
Here is a simple test program
Code:
#include <stdio.h>
#ifdef USE_MYSTRCPY_V1
void mystrcpy(char *dest, char *src)
{
while (*src != '\0') {
*dest = *src;
dest++;
src++;
}
}
#endif
#ifdef USE_MYSTRCPY_V2
void mystrcpy(char *dest, char *src)
{
int i = 0;
int j = 0;
while (src[i] != '\0') {
dest[i] = src[i];
i++;
j++;
}
}
#endif
int main(void)
{
char srcbuf[100] = "Hello, world!";
char destbuf[100] = "";
mystrcpy(destbuf, srcbuf);
printf("%s\n", destbuf);
return 0;
}
Compile with:
gcc -Wall -DUSE_MYSTRCPY_V1 -std=c99 -Os -o v1 example.c
gcc -Wall -DUSE_MYSTRCPY_V2 -std=c99 -Os -o v2 example.c
Observations:
1. On MinGW GCC 4.7.2, assembler output of "mystrcpy" for both v1 and v2 is identical.
2. Compiling with other options for -O produces longer assembly for the index version.
3. "Programming guys and gals" that know C or C++ to a usable level should find the pointer version no less readable than the index version.
4. With too many indices, one can easily prefer the pointer version to the index version.
I think it comes down to an issue of style and what makes most sense. In this example, I find the pointer version to make the most sense. I personally don't really care about observationa 1 and 2, but just included them for interest's sake.