Consider the following code:
std::vector<std::string> foo{{"blee"}, {"bleck"}, {"blah0000000000000000000000000000000000000000000000000000000000000000000000000000000000"}};
std::string *temp = foo.data();
char*** bar = reinterpret_cast<char***>(&temp);
for (size_t i = 0; i < foo.size(); ++i){
std::cout << (*bar)[i] << std::endl;
}
Clearly this is sketchy code, but it happens to work.
I would like to know why it works? Are there some strange rules of C++ I don't know about? Or is it just bad code and undefined behaviour?
I made one of the strings huge in case there was some small-string optimization going on.
Adapted from: Cast a vector of std::string to char***