我的问题是:我有一个常量指针指向一个常量 char 指针(二维字符数组,其中两个维度都是const
)。我需要将 C 字符串分配给这个数组。我有一个用于创建 c_strings 向量的 of s std::vector
。现在我将这些 C 字符串指针分配给这个数组,但我得到了std::string
c_str()
src/rshell.cpp:45:44: error: assignment of read-only location ‘*(c_arr
((sizetype)(((long unsigned int)i) * 8ul)))’
for (int i = 0; i < size1; i++) c_arr[i] = commands.at(i);
这是有错误的代码
/* Confusing as heck, let me explain!
* char const* means pointer to a constant char
* so char const* const means a const pointer to a const char
* and char const* const* means a const pointer to a const char pointer!
* (Read declarations from right to left to make it make sense -
* char const* = POINTER (*) to a CONST CHAR)
*/
char const* const* c_arr = new char*[size1];
for (int i = 0; i < size1; i++)
c_arr[i] = commands.at(i); // line 38
如果有帮助,这里是字符串到 C 字符串部分的向量。
for (tokenizer::iterator it = parse.begin(); it != parse.end(); it++)
words.push_back(*it);
vector<const char*> commands;
// add string to c_string equiv return vector
for (vector<string>::iterator it = words.begin(); it != words.end(); it++) {
commands.push_back(it->c_str());
}