Help, Im trying to implement my own strcpy

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Help, Im trying to implement my own strcpy

 
0
  #1
Dec 1st, 2008
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help, Im trying to implement my own strcpy

 
0
  #2
Dec 1st, 2008
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
}
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: Help, Im trying to implement my own strcpy

 
0
  #3
Dec 1st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help, Im trying to implement my own strcpy

 
0
  #4
Dec 2nd, 2008
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 448
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Help, Im trying to implement my own strcpy

 
0
  #5
Dec 2nd, 2008
Originally Posted by Ancient Dragon View Post
>>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

  1. char* string1 = "Bigger than String2";

wont work?
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help, Im trying to implement my own strcpy

 
0
  #6
Dec 2nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 448
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Help, Im trying to implement my own strcpy

 
0
  #7
Dec 2nd, 2008
OK.. my bad... True .. it will not work.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: Help, Im trying to implement my own strcpy

 
0
  #8
Dec 2nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help, Im trying to implement my own strcpy

 
0
  #9
Dec 2nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Help, Im trying to implement my own strcpy

 
0
  #10
Dec 2nd, 2008
Originally Posted by ArkM View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC