<<< | Index | >>> |
The char* pointer returned by c_str( ) is const
The pointer can only be relied on until the next non-const member of string is called:
#include <stdio.h> #include <string> using std::string; void main() { string file_name = "myfile.txt"; fopen( file_name, "r" ); // Will not compile: cannot convert // from std::string to const char * fopen( file_name.c_str(), "r" ); // Ok: use null-terminated char* char* backdoor = file_name.c_str(); // Will not compile, illegal const char* ptr_fname = file_name.c_str(); // Ok file_name.append( "blah" ); // Invalidates ptr_fname! }
<<< | Index | >>> |