Reson behind following strcpy protype

This is a discussion on Reson behind following strcpy protype within the C Programming forums, part of the General Programming Boards category; C standard Library have declared the strcpy protype: char * strcpy ( char * destination, const char * source ); ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    Reson behind following strcpy protype

    C standard Library have declared the strcpy protype:

    char * strcpy ( char * destination, const char * source );

    Questions:
    1. What was the reasoning/objective while returning a "char *".
    2. A "void" would have been fine?

    Thanks in advance

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    It's for convenience, so you can pass the result of strcpy as an argument to another call, like strcat for example.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Why would it return a void pointer? The thing it's pointing to will ALWAYS be a char *. Why? Because you specified it as so in the destination (which is locked into the function prototype)! strcpy will copy TO THAT DESTINATION. On return, the return value will BE that destination (i.e. the result of strcpy will be the same pointer as you gave it in destination!). You could even ask why strcpy doesn't just return a "success" value rather than returning the same pointer that you gave it (but that falls under the convenience argument mentioned above if you are lazy about checking return values are valid). If you know something is a char * and you're a string function being asked to act on that, why would you try to pretend it might be anything else and try to change the type when you return it? If the programmer wants to cast it, he can, but that data IS a char *. Returning void * would just lead to brain-damage when users think they have been given back something else.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by ledow View Post
    Why would it return a void pointer?
    Nice rant, but sanddune008 didn't suggest at all that it should be a void*

    Quote Originally Posted by sanddune008 View Post
    1. What was the reasoning/objective while returning a "char *".
    2. A "void" would have been fine?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    ....Thanks...that was simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2010, 08:42 AM
  2. strcpy without new
    By sawer in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2007, 05:34 AM
  3. strcpy
    By Abda92 in forum C Programming
    Replies: 7
    Last Post: 09-13-2006, 11:05 AM
  4. strcpy
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2002, 02:39 PM
  5. HELP!! strcpy
    By abbynormal87 in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2002, 08:34 AM

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