我有一个 const char 指针,我知道它肯定来自一个字符串。例如:
std::string myString = "Hello World!";
const char* myCstring = myString.c_str();
在我的情况下,我知道 myCstring 来自一个字符串,但我不再有权访问该字符串(我从函数调用中收到了 const char*,我无法修改函数的参数列表)。
鉴于我知道 myCstring 指向现有字符串的内容,是否有任何方法可以安全地访问它源自的父字符串的指针?例如,我可以做这样的事情吗?
std::string* hackyStringPointer = myCstring - 6; //Along with whatever pointer casting stuff may be needed
我担心的是,字符串的内容可能无法保证存储在某些或所有平台等的连续内存中。