943,913 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1971
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 1st, 2008
0

Help, Im trying to implement my own strcpy

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. void StrCpy(char * Dest , char* Src )
  2. {
  3.  
  4. while(*Src){
  5. *Dest = *Src++;
  6. cout << *Dest ++;
  7.  
  8. }
  9. }
  10.  
  11. void main()
  12. {
  13.  
  14. char* string1 = "\0";
  15. char * string2 = "EL";
  16.  
  17. StrCpy(string1, string2);
  18. //cout << string1; //must output EL
  19. }

Im trying to output EL after its copied to string1 but nothing its printed
Similar Threads
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
Dec 1st, 2008
0

Re: Help, Im trying to implement my own strcpy

You are attempting to stuff 3 bytes (string2) into a character array that is only 1 byte (string1). Try this:
int main()
{  

char string1[4] = {0}
char * string2 = "EL";

StrCpy(string1, string2);
//cout << string1; //must output EL
}
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 1st, 2008
0

Re: Help, Im trying to implement my own strcpy

it works!! .. and I heard of that before but got confused cuz the thing I did was to Initialize char * string1 = "longer than string2" and evrything else the same and it didnt work either so I thought that wasnt the problem as I thought there was a solution without need to "mix" char[] and char *
Could you tell me why using a longer string than string2 to initialize char* pointer wouldnt work in my method
I thought I would get something like this:

char* string 1 = "longer than string2"
char* string 2 = "NONSENSE"

func(string1 string2)//same as before
cout << string1 ; //I was expecting it to print changed string1 as
"NONSENSEhan string2"
in other words overwritting
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

>>I did was to Initialize char * string1 = "longer than string2" and evrything else the same and it didnt work either
I didn't work because you can't change string literals, which is what you attempted to do. But you could have done it like this:
char string1[] = "longer than string2" , which will put the text in writeable memory.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

>>I did was to Initialize char * string1 = "longer than string2" and evrything else the same and it didnt work either
I didn't work because you can't change string literals, which is what you attempted to do. But you could have done it like this:
char string1[] = "longer than string2" , which will put the text in writeable memory.

I thought you could always assign a char* to another char*. Are you sure that declaring

C++ Syntax (Toggle Plain Text)
  1. char* string1 = "Bigger than String2";

wont work?
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

And in other news, reports are coming in from all over the web about the same thing happening elsewhere.

Experts agree that there is only one possible solution.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

OK.. my bad... True .. it will not work.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

well thank you all for answering.
"and in other news".. I know .. "Have the feeling" its been answered tons of time but kinda not my fault;
I cheked many introduction texts about that and they dont say how they (the [] and the char * or the string class declarations) happend to be related.
they teach either one and/or the other as independent but not how they in this case "mix"; the do's and dont's are either one declartion or another weighted
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

Remember the K&R masterpiece:
  1. while (*dest++ = *src++);
Unforgettable Algol 68's genes are still alive in C and C++ ...
Last edited by ArkM; Dec 2nd, 2008 at 8:25 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

Click to Expand / Collapse  Quote originally posted by ArkM ...
Remember the K&R masterpiece:
  1. while (*dest++ = *src++);
Unforgettable Algol 68's genes are still alive in C and C++ ...
can I just add, you need to remember to add a
*dest=0; line after the while. You will need something similar in any of the other posts.
Last edited by Ancient Dragon; Dec 2nd, 2008 at 9:32 am. Reason: corrected icode tag
Reputation Points: 749
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Finding Positon of characters
Next Thread in C++ Forum Timeline: Need experienced debugger will pay.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC