简体   繁体   English

指向指针和函数用法的C ++ wchar_t数组

[英]C++ wchar_t array to pointer and in function usage

I had the following code: 我有以下代码:

wchar_t recordsText[64] = L"Records: ";
std::wstringstream ss2;
ss2 << c;
wcsncat_s(recordsText, ss2.str().c_str(), sizeof(ss2.str().c_str()));
((CButton*)GetDlgItem(IDC_RECORDS))->SetWindowTextW(recordsText);

It worked pretty well, but i want to put it into a function... nothing easier i thought. 它工作得很好,但是我想将其放入一个函数中……我想起来再容易不过了。 but i get an stupid error. 但我得到一个愚蠢的错误。

my function was this one: 我的职能是:

BOOL refreshTextField(CButton* item, wchar_t* label, long long* number){
    std::wstringstream ss;
    ss << number; 
    wcsncat_s(label, ss.str().c_str(), sizeof(ss.str().c_str()));
    item->SetWindowTextW(label);
    return true;
}

but the wcsncat_s doesnt like my "label" because its an array and the function is called like this: 但是wcsncat_s不喜欢我的“标签”,因为它是一个数组,并且函数的调用方式如下:

refreshTextField(((CButton*)GetDlgItem(IDC_SENT_PACKAGES)), L"Packages send:  ", &sentPackages);

(btw: i know it shouldn't be casted to a CButton because it's an edit-field :-D , but that doesnt matter at the moment.) (顺便说一句:我知道不应该将其强制转换为CButton,因为它是一个edit-field :-D,但是目前不重要。)

the problem is the wchar_t array, i dont know how to get it into my function correctly. 问题是wchar_t数组,我不知道如何正确地将其放入函数中。 hope you can give me a quit answer. 希望你能给我一个简单的答案。

i already tried this: 我已经尝试过了

BOOL refreshTextField(CButton* item, wchar_t** label, long long* number){
    //...
    wcsncat_s(*label, sizeof(*label), ss.str().c_str(), sizeof(ss.str().c_str()));
    //....
}

and this: 和这个:

BOOL refreshTextField(CButton* item, wchar_t* label, long long* number){
    //...
wcsncat_s(label, sizeof(*label), ss.str().c_str(), sizeof(ss.str().c_str()));
    //....
}

EDIT: 编辑:

So the solution was this: 因此解决方案是这样的:

call: 呼叫:

refreshTextField(mySelectedUIItem, L"testlabel", sizeof(L"testlabel"), 4);

function: 功能:

BOOL refreshTextField(CButton* item, wchar_t* label, size_t lableSize, long long* number)
{
    std::wstringstream ss;
    ss << number;
    wcsncat_s(label, labelSize, ss.str().c_str(), ss.str().length());
    //...
}

{Edit} {编辑}

When you want to use the function template, you must match all parameter types. 要使用功能模板时,必须匹配所有参数类型。 So you must pass the length of the string instead of a second copy of the c_str() result to the wcsncat_s template: 因此,必须将字符串的长度而不是c_str()结果的第二个副本传递给wcsncat_s模板:

wcsncat_s(recordsText, ss2.str().c_str(), ss2.str().length());

This will resolve to the prototype 这将解决原型

template <size_t size>
errno_t _mbsncat_s(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count
); // C++ only

{/Edit} {/编辑}

Without the template the following applies: 如果没有模板,则适用以下条件:

You can't pass an array to a function. 您不能将数组传递给函数。 The function will only accept the pointer. 该函数将仅接受指针。 The array can be well accessed with the pointer inside the function. 使用函数内部的指针可以很好地访问该数组。 But you lose the information about the array size. 但是您会丢失有关数组大小的信息。

Since the pointer only points to the first element of the array you can't use 由于指针仅指向数组的第一个元素,因此无法使用

sizeof(*somePointer);

because this gives you the size of the first array element. 因为这将为您提供第一个数组元素的大小。

You need to change the parameter list of refreshTextField. 您需要更改refreshTextField的参数列表。 Since the label argument points to an output variable you need the size of the variable as an additional parameter. 由于label参数指向输出变量,因此您需要将变量的大小作为附加参数。 eg: 例如:

BOOL refreshTextField(CButton* item, wchar_t* label, size_t lableSize, long long* number)
{
    std::wstringstream ss;
    ss << number;
    wcsncat_s(label, labelSize, ss.str().c_str(), ss.str().length());
    //...
}

sizeof(ss2.str().c_str()) sizeof(ss2.str()。c_str())

The result of function c_str() is wchar_t* . 函数c_str()的结果为wchar_t* sizeof( wchar_t* ) is 4 or 8 bytes (on 32 or 64 bit system resp.). sizeof( wchar_t* )是4或8个字节(分别针对32或64位系统)。 You should use wstring::length() function instead: 您应该改用wstring::length()函数:

wcsncat_s( label, ss.str().c_str(), ss.str().length() );

try with this 试试这个

BOOL refreshTextField(CButton* item, wchar_t[] label, long long* number){
    //...
   wcsncat_s(label, ss.str().c_str(), sizeof(ss.str().c_str()));
    //....
}

http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/ http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM