How to print array of LPCTSTR in c++?

梦想的初衷 提交于 2019-11-28 13:52:26

问题


How to print array of LPCTSTR

example

LPCTSTR pszValueNames[] = { L"partnerID", L"partnerSubID", L"transactionID" };

for (int i = 0; i < (sizeof(pszValueNames) / sizeof(LPCWSTR)); i++)
{
    cout << (*pszValueNames[i]) << endl;
}

Above give some numbers which is not real lpctstr values. When i use wchar_t* and all other basic types it spit good values.


回答1:


The reason why you get the address printed is that std::cout works with std::string, which is char based. An LPCWSTR is a wide string pointer type, and cout has no idea how to display wide strings "as strings".

You should use std::wcout to handle a wide string.

std::wcout << pszValueNames[i] << std::endl;

Also, your usage of LPCTSTR is not correct, even though your program may work. If you know you're using wide strings, specify LPCWSTR anywhere you would have used LPCTSTR. The reason is that LPCTSTR is not necessarily a wide string pointer type, depending on the type of build (MBCS or Unicode) that you are using.

So in a nutshell, your string pointer declarations are a hodge-podge of different string types, where some functions may work (if the LPCTSTR is a non-wide char pointer), while other string handling functions won't work.

Basically, if you're going to use LPCTSTR or LPTSTR, then all of your code that handles string or string pointer types should use "build-neutral" types, i.e. use LPCTSTR, TCHAR, LPTSTR, _T(), etc. These types will change seemlessly if you go from an MBCS build to a Unicode build (and vice-versa for whatever reason).

If on the other hand, if you know that the build will only ever be Unicode, then LPCWSTR and LPWSTR should be used throughout your program. Conversely, if you know and can guarantee that the build will only be MBCS, then use LPCSTR and LPSTR in your program.

The best thing to do is either use the build-neutral types (LPCTSTR, etc.) throughout your application, or change all of these to wide string types (LPCWSTR, etc.). The MBCS builds are becoming very rare these days, so might as well start developing using wide string types, and only use char string types in your application when you're interfacing to other functions that rely on char based strings.




回答2:


Two issues. First, you're iterating over the wrong size:

for (int i = 0; i < (sizeof(pszValueNames) / sizeof(LPCWSTR)); i++)
//                                                  ^^^^^^^

That should be LPCTSTR. Or, to avoid this kind of error, just *pszValueNames. Or likely the reverse, LPCWSTR is the corret type but you declared the array to be LPCTSTR instead.

Secondly:

cout << (*pszValueNames[i]) << endl;

That would dereference the ith string - which would print only the first character. To print the whole string you don't need the dereference. Also, you would need to use wcout as cout does not work with wchar_t:

wcout << pszValueNames[i] << endl;



回答3:


std::wcout was already mentioned, but I'd like to add that this is a nice example of how C++11 features can improve legacy or C-based code – if you can use a C++11 compiler, that is.

In C++11, you can write your loop like this:

for (auto const &name : pszValueNames)
{
    std::wcout << name << std::endl;
}

No more sizeof or dereferencing issues to worry about. And if you later change your array to something like std::array<std::wstring> or std::vector<std::wstring> or std::set<std::wstring>, then the loop will not have to be modified at all (the element type of the container will just have to remain compatible with std::wcout).


edit: made code example use const &




回答4:


The [] operator accesses the index*sizeof(type) part of the memory after the pointed mem. value, and this operator takes a pointer. You dont need the * there... *pszValue is dereferencing, true, but that is done implicitly here.... so you are actually dereferencing a value...

Quote from MSDN: "Usually, the value represented by postfix-expression is a pointer value, such as an array identifier, and expression is an integral value (including enumerated types). However, all that is required syntactically is that one of the expressions be of pointer type and the other be of integral type."

See the [] operator here



来源:https://stackoverflow.com/questions/28481428/how-to-print-array-of-lpctstr-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!