| | |
Help, Im trying to implement my own strcpy
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
That is a problem with all standard C functions. If you use one of the new Microsoft compilers, Microsoft declared them depreciated and wants us to use the _s functions, such as strcpy_s(), which has an additional destination buffer size parameter. But even that could cause buffer overflow if the calling function passed the wrong buffer size. There is no 100% bullet-proof method in C to prevent buffer overflow. But if you follow the rules and bother to actually read the function's requirements then you can minimize the buffer overflow problem.
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
I modified the program it looks like this but no doubt theres something wrong as it wont output anything I await your wisdom guys
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> using std::cin; using std::cout; void StrCpy(char * Dest , char* Src ) { int i; char *tmp = new char[strlen(Src)+1]; for(i = 0 ; *Src!='\0' ; Src++) tmp[i]=*Src; tmp[i]='\0'; Dest = tmp; for(int i= 0 ; tmp[i]!='\0' ; i++) cout << tmp[i]; } void main() { char *string1; char * string2 = "EL"; StrCpy(string1, string2); // while(*string1) // cout << *string1++; }
Your version doesn't work because the first parameter Dest needs to be passed by pointer, not by value
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> using std::cin; using std::cout; void StrCpy(char ** Dest , char* Src ) { int i; char *tmp = new char[strlen(Src)+1]; for(i = 0 ; *Src!='\0' ; Src++) tmp[i]=*Src; tmp[i]='\0'; *Dest = tmp; for(int i= 0 ; tmp[i]!='\0' ; i++) cout << tmp[i]; } void main() { char *string1 = 0; char * string2 = "EL"; StrCpy( &string1, string2); // while(*string1) // cout << *string1++; }
Last edited by Ancient Dragon; Dec 2nd, 2008 at 10:55 pm.
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.
C++ Syntax (Toggle Plain Text)
for(i = 0 ; *Src!='\0' ; Src++) tmp[i]=*Src;
should increment 'i' also. as of now 'i' remains 0 throughout.
thanks
-chandra
-chandra
![]() |
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 based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux 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 return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






