How to convert type std :: basic_string to an array of type char?

When you run this code, the following error occurs:

syslog(LOG_ERR | LOG_USER, "%s",errorString); 

cannot convert 'const string {aka const std :: basic_string} to' const char * for> argument '2 to' void syslog (int, const char *, ...) inServer.cpp / PeCounter
line 478 C / C ++ problem

I unmount the program, and the errorString value prints fine when output to stdio using cout, but when using the syslog call it will not print.

Any way to get std :: basic_string (char) as 'const char'.

+6
source share
2 answers

I found that std::basic_string has a method to access the c_str() element, which seems to fix the compilation problem.

Here is a site with more information: http://en.cppreference.com/w/cpp/string/basic_string

+6
source

The c_str() or data() member function provides a pointer to the first element of the char_type array that contains your string. It is valid as long as the string object itself remains valid and unchanged (but be careful that operations that may cause redistribution may invalidate the pointer - it is better not to store it).

+4
source

Source: https://habr.com/ru/post/928133/


All Articles