#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    Redding
    Posts
    49
    Rep Power
    12

    Need to Rewrite strcpy() function


    We have a new assignment: rewrite the strcpy() function that is usually accessable with the <cstring.h> include. I thought of a way of doing it with the use of pointers however, my instructor has outlawed them because we have not reached that lesson yet. If someone here can provide a hint or a solution I belive that I would learn alot from it. So, strcpy(dest, origin); no pointers any suggestions?

    Thanks in advance.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Southern California
    Posts
    73
    Rep Power
    15
    How about this-- no pointers:

    Code:
    #include <string.h>
    #include <stdio.h>
    
    char * mystrcpy(char to[], const char from[], size_t size)
    {
        register int i;
    
        for (i=0; i <= size; i++)
            to[i] = from[i];
    
        to[size] = 0;
        return to;
    }
    
    int main (int argc, char** argv)
    {
        char foo[] = {'f','o','o'};
        char bar[4];
    
        (void)mystrcpy(bar, foo, 3);
        printf("bar = %s\n", bar);
        return 0;
    }
  4. #3
  5. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Or, without a size parameter:

    Code:
    void mystrcpy (char to[], char from[])
    {
        int i;
    
        for (i = 0; from[i] != 0; i++) {
            to[i] = from[i];
        }
    }
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    Redding
    Posts
    49
    Rep Power
    12
    That's not a pointer?
    What is the astric after the return value in the function mean?
    ////////////////////////////////////////////////////////
    char * mystrcpy(char to[], const char from[], size_t size)
    {
    register int i;

    for (i=0; i <= size; i++)
    to[i] = from[i];

    to[size] = 0;
    return to;
    }
    ///////////////////////////////////////////////////////
    I already gave up and used a pointer. The assignmentis due today....I just had a thought, is that a virtual function? If it is, were not allowed to use those either :-)
    Thanks anyway though.
  8. #5
  9. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    What is the astric after the return value in the function mean?
    The asterisk is part of the return type. vpopper's mystrcpy() function actually returns a pointer-to-char. With parentheses, the prototype would be

    Code:
    (char *) mystrcpy (char to[], const char from[], size_t size);
    That's not a pointer?
    I'm not sure what exactly you're referring to, but I'll assume you're talking about the functions' parameters. Strictly speaking they are pointers, yes. The []-brackets are more a hint to the programmer that the function expects an array-of-type rather than a pointer-to-type.

    Although a bit late (since you already handed in your assignment), I think the idea was that you didn't use pointer arithmetic to copy the string.

    The thing is, it doesn't make sense to write a string copying routine if you're not working with the original destination array. A reference to it has to be passed to the function one way or another. C implements the "pass by reference" concept with pointers; there's no way around that.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/

IMN logo majestic logo threadwatch logo seochat tools logo