Can anybody tell me what is the use of c_str() function in C/C++?. In which case it is necessary to use it?.
what is use of C_str() function in C/C++
11.6k Views Asked by sachin At
4
There are 4 best solutions below
0

Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.
There is a good example of its use here: http://www.cplusplus.com/reference/string/string/c_str/
It is a C++ thing, not a C one.
A common use of c_str (from
std::string
) is precisely to convert a C++std::string
to aconst char*
C string, which is required by many many low level C functions (e.g. Posix system calls likestat
, etc).