更改 wchar_t* [] 中的值
changing values in wchar_t* []
我有这个代码:
char *charTable[] = { "test1", "test2", "test3" };
size_t originSize[] = { 6, 6, 6 };
wchar_t* textValues[3];
const size_t newsize = 100;
for (int i = 0; i < 3; i++) {
wchar_t wcstring[newsize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, originSize[i], charTable[i], _TRUNCATE);
wcscat_s(wcstring, L"");
textValues[i] = wcstring;
}
我想把 "test1"
、"test2"
、"test3"
放在 textValues
中作为 wchar_t*
,但是在循环 textValues
之后包含 "test3"
, "test3"
, "test3"
.
首先,您需要将目标指针初始化为某物:
textValues[i] = new wchar_t[newsize];
然后,使用 wcscpy
:
而不是分配 textValues[i] = wcstring;
(这会生成一个浅拷贝)
wcscpy(textValues[i], wcstring);
或者使用更安全的wcscpy_s
:
wcscpy_s(textValues[i], newsize, wcstring);
最后,记得delete
用完新分配的内存:
delete [] textValues[i];
完整样本:
char *charTable[] = { "test1", "test2", "test3" };
size_t originSize[] = { 6, 6, 6 };
wchar_t* textValues[3];
const size_t newsize = 100;
for (int i = 0; i < 3; i++) {
wchar_t wcstring[newsize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, originSize[i], charTable[i], _TRUNCATE);
wcscat_s(wcstring, L"");
textValues[i] = new wchar_t[newsize];
wcscpy_s(textValues[i], newsize, wcstring);
}
//later
for (int i = 0; i < 3; i++) {
delete [] textValues[i];
}
我有这个代码:
char *charTable[] = { "test1", "test2", "test3" };
size_t originSize[] = { 6, 6, 6 };
wchar_t* textValues[3];
const size_t newsize = 100;
for (int i = 0; i < 3; i++) {
wchar_t wcstring[newsize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, originSize[i], charTable[i], _TRUNCATE);
wcscat_s(wcstring, L"");
textValues[i] = wcstring;
}
我想把 "test1"
、"test2"
、"test3"
放在 textValues
中作为 wchar_t*
,但是在循环 textValues
之后包含 "test3"
, "test3"
, "test3"
.
首先,您需要将目标指针初始化为某物:
textValues[i] = new wchar_t[newsize];
然后,使用 wcscpy
:
textValues[i] = wcstring;
(这会生成一个浅拷贝)
wcscpy(textValues[i], wcstring);
或者使用更安全的wcscpy_s
:
wcscpy_s(textValues[i], newsize, wcstring);
最后,记得delete
用完新分配的内存:
delete [] textValues[i];
完整样本:
char *charTable[] = { "test1", "test2", "test3" };
size_t originSize[] = { 6, 6, 6 };
wchar_t* textValues[3];
const size_t newsize = 100;
for (int i = 0; i < 3; i++) {
wchar_t wcstring[newsize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, originSize[i], charTable[i], _TRUNCATE);
wcscat_s(wcstring, L"");
textValues[i] = new wchar_t[newsize];
wcscpy_s(textValues[i], newsize, wcstring);
}
//later
for (int i = 0; i < 3; i++) {
delete [] textValues[i];
}