是否可以更改 wchar_t* 的内容?

Is it possible to change the content of a wchar_t*?

我有一个接收名为 somestringwchar_t* 参数的函数。

我想调整 wchar_t* somestring 的大小以包含比传递的更多的元素,因为我想复制比原始字符串更大的内容:

void a_function(wchar_t *somestring) {
    // I'd like the somestring could contain the following string:
    somestring = L"0123456789"; // this doesn't work bebause it's another pointer
}

int _tmain(int argc, _TCHAR* argv[])
{
    wchar_t *somestring = L"0123";
    a_function(somestring);
    // now I'd like the somestring could have another string at the same somestring variable, it should have the following characters: "0123456789"
}

是否可以调整大小 wchar_t *somestring 以包含更多字符?

如您所见,void a_function(wchar_t *somestring) { somestring = L"0123456789";} 只是更改局部指针变量 somestring,对调用者没有任何影响。交换调用者的指针值需要传递一个指向这个指针的指针:

void a_function(const wchar_t **somestring) {
  *somestring = L"0123456789";
}

int main(int argc, char* argv[]) {
    const wchar_t *somestring = L"0123";
    a_function(&somestring);
    // somestring now points to string literal L"0123456789";
}

请注意,通过覆盖指针,您失去了对字符串文字 L"0123" 的引用。 进一步注意数据类型现在是 const wchar_t*,否则不应分配字符串文字(不允许修改字符串文字的内容)。