是否可以更改wchar_t*的内容
Is it possible to change the content of a wchar_t*?
我有一个函数,它接收一个名为somestring
的wchar_t*
参数。
我想调整wchar_t* somestring
的大小以包含比传递的更多的元素,b因为我想复制比原始字符串更大的内容:
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*
,因为否则不应该分配字符串文字(不允许修改字符串文字的内容(。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- C/C++预处理器是否可以检测一些编译器选项
- 检查 WCHAR 是否包含字符串