------------------------------------------------------------------------------
MC logo
String Copy Implementation II
[^] Code Examples
------------------------------------------------------------------------------
<<String Copy Implementation I strcpy2.c String Copy Implementation III>>
/*
 * An implementation of strcpy using pointer arithmatic.
 */
char *strcpy(char *target, const char *source)
{
        char *orig_target = target;

        while(*source)
                *target++ = *source++;
        *target = '\0';

        return orig_target;
}

#include <stdio.h>
int main()
{
        char s1[] = "Hi there, fred!";
        char s2[40];

        strcpy(s2, s1);
        printf("%s\n", s2);
}

This version uses pointer arithmetic. It also takes advantage of the fact that the string terminator character, \0, converts to integer zero, which is false, while all other characters convert to non-zero ASCII codes, which are true.
Reading: pp. 58-66
<<String Copy Implementation I String Copy Implementation III>>