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
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
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
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.
....Thanks...that was simple.