[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppStrcpy
The example below copies a char* over another.
Use std::string instead of an array of char [1,2].
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppStrcpy
(C++) std::strcpy
The standard function std::strcpyt copies char*'s (i.e. C-style-strings, or null-terminated strings).The example below copies a char* over another.
int main() { char * const name1 = "Bilderbikkel"; char * const const name2 = "was here...."; //assert(name1=="Bilderbikkel"); //Won't work, only for std::strings assert(std::strcmp(name1,"Bilderbikkel")==0); //Copy name2 over name1 std::strcpy(name1,name2); //assert(name1=="was here...."); //Won't work, only for std::strings assert(std::strcmp(name1,"was here....")==0); }
- include <cstring>
- include <cassert>
Use std::string instead of an array of char [1,2].
Code links
- #include
- cassert (header file)
- char
- const
- cstring (header file)
- include, #include
- int
- main
- std::strcmp
- strcmp, std::strcmp
'strcpy' links
- ?C
Reference
- 1) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4 Chapter 5.8.5: 'Use string rather then zero-terminated arrays of char'.
- 2) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 77: 'Use vector and string instead of arrays.'
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
