[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppStrrchr
To obtain a pointer to the first location of a char in a C-style string, use std::strchr.
Use std::string instead of an array of char [1,2].
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppStrrchr
(C++) std::strrchr
The standard function strrchr returns a pointer to the last location of a char in a C-style string. If the char is not found, it returns null.To obtain a pointer to the first location of a char in a C-style string, use std::strchr.
Use std::string instead of an array of char [1,2].
Declaration
char * strrchr ( const char *, int );
Example
int main() { const char myString[] = "Bilderbikkel"; // Find the first 'k' const char * const myPointer = std::strrchr(myString,'k'); // Check that it indeed points to a 'k' assert(*myPointer == 'k'); // Check that it indeed points to the last 'k', // by checking that the previous position is 'k' assert(* (myPointer-1) == 'k'); }
- include <cstring>
- include <cassert>
'strrchr' links
- ?C
Code links
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]
