The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Need to Rewrite strcpy() function
Discuss Need to Rewrite strcpy() function in the C Programming forum on Dev Shed. Need to Rewrite strcpy() function C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 10th, 2002, 09:49 PM
|
|
Contributing User
|
|
Join Date: Aug 2002
Location: Redding
Posts: 49
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
Need to Rewrite strcpy() function
We have a new assignment: rewrite the strcpy() function that is usually accessable with the <cstring.h> include. I thought of a way of doing it with the use of pointers however, my instructor has outlawed them because we have not reached that lesson yet. If someone here can provide a hint or a solution I belive that I would learn alot from it. So, strcpy(dest, origin); no pointers any suggestions?
Thanks in advance.
|

November 18th, 2002, 02:12 AM
|
 |
Contributing User
|
|
Join Date: Jun 2000
Location: Southern California
Posts: 73
Time spent in forums: 56 m 9 sec
Reputation Power: 13
|
|
How about this-- no pointers:
Code:
#include <string.h>
#include <stdio.h>
char * mystrcpy(char to[], const char from[], size_t size)
{
register int i;
for (i=0; i <= size; i++)
to[i] = from[i];
to[size] = 0;
return to;
}
int main (int argc, char** argv)
{
char foo[] = {'f','o','o'};
char bar[4];
(void)mystrcpy(bar, foo, 3);
printf("bar = %s\n", bar);
return 0;
}
|

November 18th, 2002, 03:22 AM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
Or, without a size parameter:
Code:
void mystrcpy (char to[], char from[])
{
int i;
for (i = 0; from[i] != 0; i++) {
to[i] = from[i];
}
}
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/
|

November 18th, 2002, 02:07 PM
|
|
Contributing User
|
|
Join Date: Aug 2002
Location: Redding
Posts: 49
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
That's not a pointer?
What is the astric after the return value in the function mean?
////////////////////////////////////////////////////////
char * mystrcpy(char to[], const char from[], size_t size)
{
register int i;
for (i=0; i <= size; i++)
to[i] = from[i];
to[size] = 0;
return to;
}
///////////////////////////////////////////////////////
I already gave up and used a pointer. The assignmentis due today....I just had a thought, is that a virtual function? If it is, were not allowed to use those either :-)
Thanks anyway though.
|

November 19th, 2002, 03:38 AM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
Quote: | What is the astric after the return value in the function mean? |
The asterisk is part of the return type. vpopper's mystrcpy() function actually returns a pointer-to-char. With parentheses, the prototype would be
Code:
(char *) mystrcpy (char to[], const char from[], size_t size);
I'm not sure what exactly you're referring to, but I'll assume you're talking about the functions' parameters. Strictly speaking they are pointers, yes. The []-brackets are more a hint to the programmer that the function expects an array-of-type rather than a pointer-to-type.
Although a bit late (since you already handed in your assignment), I think the idea was that you didn't use pointer arithmetic to copy the string.
The thing is, it doesn't make sense to write a string copying routine if you're not working with the original destination array. A reference to it has to be passed to the function one way or another. C implements the "pass by reference" concept with pointers; there's no way around that.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|