|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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;
}
|
|
#3
|
||||
|
||||
|
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/ |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
||||
|
||||
|
Quote:
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); Quote:
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Need to Rewrite strcpy() function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|