strcpy - my implementation

A

arnuld

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>
#include <unistd.h>
#include <string.h>

enum { ARRSIZE = 101 };

char* my_strcpy( char*, 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 );


if( 2 != argc )
{
perror("USAGE: ./exec \" your input \"\n");
exit( EXIT_FAILURE );
}
else
{
strcpy( arr_in , argv[1] );
}

pc = my_strcpy( arr_out, arr_in );



while( *pc )
{
printf("*pc = %c\n", *pc++);
}

return EXIT_SUCCESS;
}



char* my_strcpy( char* arr_out, char* arr_in )
{
char* pc;

pc = arr_out;

while( (*arr_out++ = *arr_in++) ) ;

return pc;
}


=============== OUTPUT ======================

[arnuld@dune ztest]$ gcc -ansi -pedantic -Wall -Wextra check_STRCPY.c
[arnuld@dune ztest]$ ./a.out like
*pc = l
*pc = i
*pc = k
*pc = e
[arnuld@dune ztest]$

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. I really did not understand it.
Using thr array name will give a pointer to its 1st element but int htis
case it is giving a pointer to its last element. Why ? Thats why I
introduced the extra "char* pc" in first place.
 
V

viza

Hi

/* My version of "strcpy - a C Library Function */
char* my_strcpy( char* arr_out, char* arr_in ) {

That should be: char *strcpy(char *dest, const char *src);
char* pc;
pc = arr_out;
while( (*arr_out++ = *arr_in++) ) ;
return pc;
}
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. I really did not
understand it. Using thr array name will give a pointer to its 1st
element but int htis case it is giving a pointer to its last element.
Why ? Thats why I introduced the extra "char* pc" in first place.

arr_in is not an array, it is a pointer. If you increment the pointer
repeatedly it will be a pointer to a later address in memory. In this
case, at the end of the function it will point past the end of the string.

Your implementation is much slower than what that would be used in almost
any real c library, because it copies one byte at a time. If you know
something more about the way the target processor works than the C
standard tells you, then you can copy several bytes at a time using (for
example) int.

HTH
viza
 
I

Ian Collins

arnuld said:
char* my_strcpy( char* arr_out, char* arr_in )
{
char* pc;

pc = arr_out;

while( (*arr_out++ = *arr_in++) ) ;

return pc;
}

As has been pointed out elsewhere, the source pointer should be const
and the names don't make sense. The inner parentheses in the while loop
are superfluous.
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. I really did not understand it.
Using thr array name will give a pointer to its 1st element but int htis
case it is giving a pointer to its last element. Why ? Thats why I
introduced the extra "char* pc" in first place.
Because you have incremented it to point the the '\0' at the end of the
string.
 
I

Ian Collins

arnuld said:
char* my_strcpy( char* arr_out, char* arr_in )
{
char* pc;

pc = arr_out;

while( (*arr_out++ = *arr_in++) ) ;

return pc;
}

As has been pointed out elsewhere, the source pointer should be const
and the names don't make sense. The inner parentheses in the while loop
are superfluous.
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. I really did not understand it.
Using thr array name will give a pointer to its 1st element but int htis
case it is giving a pointer to its last element. Why ? Thats why I
introduced the extra "char* pc" in first place.
Because you have incremented it to point the the '\0' at the end of the
string.
 
R

Richard

viza said:
Hi




That should be: char *strcpy(char *dest, const char *src);

Oh for goodness sake.
arr_in is not an array, it is a pointer. If you increment the pointer
repeatedly it will be a pointer to a later address in memory. In this

If you increment it once it will be a pointer to a later address in
memory...
case, at the end of the function it will point past the end of the
string.

So what?
Your implementation is much slower than what that would be used in
almost

Much slower?
any real c library, because it copies one byte at a time. If you know
something more about the way the target processor works than the C
standard tells you, then you can copy several bytes at a time using (for
example) int.

HTH
viza

I would be interested to hear how you would do this in standard C.
 
R

Richard

viza said:
Hi




That should be: char *strcpy(char *dest, const char *src);

Oh for goodness sake.
arr_in is not an array, it is a pointer. If you increment the pointer
repeatedly it will be a pointer to a later address in memory. In this

If you increment it once it will be a pointer to a later address in
memory...
case, at the end of the function it will point past the end of the
string.

So what?
Your implementation is much slower than what that would be used in
almost

Much slower?
any real c library, because it copies one byte at a time. If you know
something more about the way the target processor works than the C
standard tells you, then you can copy several bytes at a time using (for
example) int.

HTH
viza

I would be interested to hear how you would do this in standard C.
 
R

Richard

Ian Collins said:
Now you are being an arse, the correction is correct. The second
pointer points to an invariant.

I was referring to the name change.
He's answering the question, boy you are dense tonight..

I am pointing out that it has zero impact. So nothing to worry
about. But I could have, and should have, been more explicit.
 
R

Richard

Ian Collins said:
Now you are being an arse, the correction is correct. The second
pointer points to an invariant.

I was referring to the name change.
He's answering the question, boy you are dense tonight..

I am pointing out that it has zero impact. So nothing to worry
about. But I could have, and should have, been more explicit.
 
V

vippstar

Now you are being an arse, the correction is correct. The second
pointer points to an invariant.

No, it's wrong.
It's C code, not a C implementation, therefore it shouldn't have the
name strcpy. (a hint that it's C code is that he includes several
standard header files, ignoring <unistd.h> and that he has main there)
 
V

vippstar

Now you are being an arse, the correction is correct. The second
pointer points to an invariant.

No, it's wrong.
It's C code, not a C implementation, therefore it shouldn't have the
name strcpy. (a hint that it's C code is that he includes several
standard header files, ignoring <unistd.h> and that he has main there)
 
B

Bartc

arnuld said:
I have created my own implementation of strcpy library function. I would
like to have comments for improvements:
char* my_strcpy( char* arr_out, char* arr_in )
{
char* pc;
if (arr_out==NULL||arr_in==NULL)return "";

Testing for null strings might be useful; either return empty string or
abort. But not everyone agrees.
pc = arr_out;

while( (*arr_out++ = *arr_in++) ) ;

If you're interested in speed you might like to test your version against
the standard strcpy, for various lengths of strings and copying a few
million times.

You might then also like to test a version using strlen() and memcpy().
 
B

Bartc

arnuld said:
I have created my own implementation of strcpy library function. I would
like to have comments for improvements:
char* my_strcpy( char* arr_out, char* arr_in )
{
char* pc;
if (arr_out==NULL||arr_in==NULL)return "";

Testing for null strings might be useful; either return empty string or
abort. But not everyone agrees.
pc = arr_out;

while( (*arr_out++ = *arr_in++) ) ;

If you're interested in speed you might like to test your version against
the standard strcpy, for various lengths of strings and copying a few
million times.

You might then also like to test a version using strlen() and memcpy().
 
S

Sjoerd

char arr_in[ARRSIZE] = {0};
char arr_out[ARRSIZE] = {0};

What does this do? How does it work? You seem to initialize only one
element of the array, so I expect that it does not set the whole content
of the array to 0.
 
S

Sjoerd

char arr_in[ARRSIZE] = {0};
char arr_out[ARRSIZE] = {0};

What does this do? How does it work? You seem to initialize only one
element of the array, so I expect that it does not set the whole content
of the array to 0.
 
J

James Kuyper

Sjoerd said:
char arr_in[ARRSIZE] = {0};
char arr_out[ARRSIZE] = {0};

What does this do? How does it work? You seem to initialize only one
element of the array, so I expect that it does not set the whole content
of the array to 0.

Your expectation is incorrect. If you explicitly initialize any elements
of an array or any members of a structure, all of the remaining array
elements or structure members are implicitly zero-initialized.
 
J

James Kuyper

Sjoerd said:
char arr_in[ARRSIZE] = {0};
char arr_out[ARRSIZE] = {0};

What does this do? How does it work? You seem to initialize only one
element of the array, so I expect that it does not set the whole content
of the array to 0.

Your expectation is incorrect. If you explicitly initialize any elements
of an array or any members of a structure, all of the remaining array
elements or structure members are implicitly zero-initialized.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

union, strcpy and main() 10
sorting char array 13
Pointer Arithmetic Problem 22
Binary Search in C 7
Command Line Arguments 0
struct inside struct 5
Print with command-line arguments 0
Copying string till newline 23

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top