| | |
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:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
void StrCpy(char * Dest , char* Src ) { while(*Src){ *Dest = *Src++; cout << *Dest ++; } } void main() { char* string1 = "\0"; char * string2 = "EL"; StrCpy(string1, string2); //cout << string1; //must output EL }
Im trying to output EL after its copied to string1 but nothing its printed
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.
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 1
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
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
>>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:
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.
•
•
•
•
>>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)
char* string1 = "Bigger than String2";
wont work?
thanks
-chandra
-chandra
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.
Experts agree that there is only one possible solution.
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 1
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
"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
Remember the K&R masterpiece:
Unforgettable Algol 68's genes are still alive in C and C++
...
c Syntax (Toggle Plain Text)
while (*dest++ = *src++);
... Last edited by ArkM; Dec 2nd, 2008 at 8:25 am.
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
•
•
•
•
Remember the K&R masterpiece:
Unforgettable Algol 68's genes are still alive in C and C++c Syntax (Toggle Plain Text)
while (*dest++ = *src++);...
*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
![]() |
Similar Threads
- access Windows Address Book (C)
- Pointers (archived tutorial) (C++)
- need help with tricky string modyfication (C)
- copying unknown string lenghts (C)
- adding to a string (C++)
- Pointers (C++)
- No idea how to do this problem... (C++)
- sorting 2d arrays (C)
Other Threads in the C++ Forum
- Previous Thread: Finding Positon of characters
- Next Thread: Need experienced debugger will pay.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






