Pointer Question

This is a discussion on Pointer Question within the C Programming forums, part of the General Programming Boards category; I have been reading a tutorial dealing with functions and it shows the example code: char *my_strcpy(char *destination, char *source) ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    6

    Pointer Question

    I have been reading a tutorial dealing with functions and it shows the example code:

    char *my_strcpy(char *destination, char *source)
    {
    char *p = destination;
    while (*source != '\0')
    {
    *p++ = *source++;
    }
    *p = '\0';
    return destination;
    }


    Although it shows no reason why it makes my_strcpy a pointer (char *my_strcyp(char *dest......)). What is the use of this, Is the astrik before my_strcyp just there since there are pointers in the parameters (char *destination, char *source) and one last thing....why do they say: char *p = destination, couldn't they just use destination instead of declaring another pointer that points to it?

    Thanks a lot.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Although it shows no reason why it makes my_strcpy a pointer (char *my_strcyp(char *dest......)). What is the use of this, Is the astrik before my_strcyp just there since there are pointers in the parameters (char *destination, char *source) and one last thing....why do they say: char *p = destination, couldn't they just use destination instead of declaring another pointer that points to it?
    my_strcpy returns a char * (aka a null terminated string). that's why. it says
    Code:
    char *p = destination;
    because p is pointing to a different character as the loop progresses, while destination's address doesn't change. p is merely a temp.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    why is the function itself a pointer?
    That really doesn't make sense to me.
    Does it work?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    The function itself isn't a pointer: it just returns one.

    The specification for strcpy is that it copies the contents of one string into another and returns the pointer to the first character in the destination string. In answer to a question upthread, the reason why they don't use destination directly (that it's value is assigned to p and then p is used for the copy operation) is to preserve it's value so it can be returned at the end.

    Ian Woods

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    6

    Thanks

    I understand now.....thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21