How to print array of LPCTSTR in c++?


How to print array of LPCTSTR


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.


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.


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.


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;


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).

<hr />

edit: made code example use const &


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 <a href="https://msdn.microsoft.com/en-us/library/th52k9c8.aspx" rel="nofollow">here</a>


  • iostream, wifstream, and eclipse/g++ on windows
  • How to Avoid Multiple Instances of Different Users but Allow Multiple Instances on Single User Sessi
  • Using decltype with member function pointers
  • Get path to all users' start menu on Windows
  • How can I give “Japanese/Chinese” data?
  • Why I don't see a question mark when I use WS_EX_CONTEXTHELP?
  • HttpWebRequest redirect with cookies
  • Producing content indefinitely in a separate thread for all connections?
  • Get Deleted files list from Git Commits
  • System.OutOfMemoryException: Out of memory (GDI)
  • How do i read EXIF data from an image without the use of external scripts in python?
  • Data table - apply the same function on several columns to create new data table columns
  • How to find the first character in a given string, that appears only once [closed]
  • Character strings in Fortran: Portable LEN_TRIM and LNBLNK?
  • dangers of heap overflows?
  • For a np.array([1, 2, 3]) why is the shape (3,) instead of (3,1)? [duplicate]
  • Android acting as a wifi server-hub?
  • Word 2007 VBA: ActiveDocument.CustomXMLParts
  • Finding references in Visual Studio
  • Why is this generic method unsafe?
  • SQL complicated recursive CTE
  • Save and retrieve an image file in LibGDX
  • How do I include superscripts in NSString?
  • How can I configure a Tkinter widget from a separate class?
  • Delphi. Analog of Memo/RichEdit
  • How to remove last utf8 char of a python string
  • Running jasmine tests for a component with NgZone dependency
  • What Makes These Two Array Adds Different?
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • Tell Git to stop prompting me for conflicts when none really exist?
  • Bypass multiple inheritance in Java
  • jQuery ready not fired after rails link_to is clicked
  • Yii2: Config params vs. const/define
  • Algorithm for a smudge tool?
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • swift auto completion not working in Xcode6-Beta
  • Convert array of 8 bytes to signed long in C++
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Net Present Value in Excel for Grouped Recurring CF