C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old November 10th, 2002, 09:49 PM
Rdesign Rdesign is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Redding
Posts: 49 Rdesign User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #2  
Old November 18th, 2002, 02:12 AM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
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;
}

Reply With Quote
  #3  
Old November 18th, 2002, 03:22 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 27 m 53 sec
Reputation Power: 9
Send a message via ICQ to Analyser
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/

Reply With Quote
  #4  
Old November 18th, 2002, 02:07 PM
Rdesign Rdesign is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Redding
Posts: 49 Rdesign User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #5  
Old November 19th, 2002, 03:38 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 27 m 53 sec
Reputation Power: 9
Send a message via ICQ to Analyser
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);


Quote:
That's not a pointer?


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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need to Rewrite strcpy() function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway