Sunday, August 2, 2009

C programming: strcpy?

does strcpy make an actual copy of a string or just a pointer to the string that you are trying to copy? i'm writing a program where i pass a string into a method and the method uses strtok on the string i pass. however strtok modifies the string, which i don't want it to do. i guess i'm having trouble with my pointers and i thought strcpy might fix it but i'm still having the same problem. any suggestions on how to stop the method from modifying the string?

C programming: strcpy?
strcpy does make a deep copy of a c string. In fact, it copies the original string into the destination buffer, without much checking. For more on strcpy, look at http://www.cplusplus.com/reference/clibr...





As for processing your string you have 2 options. One is to make a copy of the original strign and use strtok on the modified string, like you attempted. Another alternative is to implement, from scrath, the parsing functionality you desire, without modifying the original string.





PS: For security reasons, depending on what this code is for, I would not use strcpy. strncpy and even memcpy are safer.


No comments:

Post a Comment