Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
例如:
string str[3]; void foo(char** str) { //do something to str... }
如何以方便的方式将 str[] 传递给函数 foo?
该函数需要一个指针数组,因此您必须从字符串数组中创建一个:
std::vector<char*> pointers; for (auto & s : str) { pointers.push_back(&s[0]); } foo(&pointers[0]);
请注意,如果函数修改了指针或它们指向的字符串,这可能无效。如果可能,更好的选择是避免混合 C 和 C++ 样式的字符串处理。