arnuld said:
I have created my own implementation of strcpy library function. I would
like to have comments for improvements:
/* My version of "strcpy - a C Library Function */
#include <stdio.h>
#include <stdlib.h>
I understand that you might need these for testing purposes...
#include <unistd.h>
....but why on earth do you need this non-standard header for testing an
implementation of a standard and rather simple C function?
#include <string.h>
Strange that you should need this, but okay.
enum { ARRSIZE = 101 };
char* my_strcpy( char*, char* );
Better: char *my_strcpy(char *, const char *);
>
int main( int argc, char** argv )
{
char* pc;
char arr_in[ARRSIZE];
char arr_out[ARRSIZE];
memset( arr_in, '\0', ARRSIZE );
memset( arr_out, '\0', ARRSIZE );
Or just:
char arr_in[ARRSIZE] = {0};
char arr_out[ARRSIZE] = {0};
which saves you two memset calls.
if( 2 != argc )
{
perror("USAGE: ./exec \" your input \"\n");
exit( EXIT_FAILURE );
}
else
{
strcpy( arr_in , argv[1] );
What if argv[1] is longer than 100 bytes? You should test for this.
<snip>
char* my_strcpy( char* arr_out, char* arr_in )
Better: char *my_strcpy(char * arr_out, const char *arr_in)
{
char* pc;
pc = arr_out;
while( (*arr_out++ = *arr_in++) ) ;
return pc;
}
How is this *your* implementation? It isn't significantly different from
the implementation on p105 of K&R2, with names changed to protect the
innocent and a return value added to get a closer match to ISO strcpy.
<snip>
It works fine without troubles. Now if you change the last return call in
my_strcpy from "return pc" to return "return arr_out", then while loop in
main() will not print anything at all.
arr_out is badly named. In my_strcpy, it's not an array, but a pointer.
Consider what value that pointer has at the time you've completed the loop.
To which byte is it pointing? What value does that byte have? And
consequently, if you return it to main and capture that pointer value in
pc = my_strcpy(arr_o ut, arr_in), to what byte is pc pointing?
You would find it easier to learn and we would find it easier to explain if
you chose different names in called functions from those in calling
functions. When discussing your program, I can't refer to 'pc' without
saying which pc I mean. Nor can I refer to arr_in or arr_out without
saying which I mean. Had you used different names in my_strcpy, it would
have made discussion easier.
I really did not understand it.
Using thr array name will give a pointer to its 1st element
Yes. And that's what my_strcpy's arr_in is - a copy of a pointer to the
first element of an array. But then *you* change the value of that
pointer.
but int htis
case it is giving a pointer to its last element. Why ?
If you change an object's value, you should not be surprised if that
object's value changes.
Thats why I introduced the extra "char* pc" in first place.
Yes. It's necessary in this case. Well, there is another way round,
actually:
char *my_strcpy(char *target, const char *source)
{
size_t i = 0;
while(target[i] = source[i])
{
++i;
}
return target;
}
which avoids the extra pointer, but only at the expense of an extra size_t.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999