It's not safe but you could call the library function using a properly sized string s -
test_func(reinterpret_cast<const char(*)[4]>(s.c_str()));
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have the following code:
void test_func(const char(*str)[4])
{
std::cout << *str << std::endl;
}
caller code:
std::string s{"abc"};
char c[4];
strcpy_s(c, s.c_str());
test_func(&c);
It is possible to replace char[4] with modern std::vector<char> ? If it is, how can I do that ?
I have tried a silly code:
std::vector<char> chars(s.cbegin(), s.cend());
test_func(&chars[0]);
Of course, didn't work it.
It's not safe but you could call the library function using a properly sized string s -
test_func(reinterpret_cast<const char(*)[4]>(s.c_str()));