JJ McKool
JJ McKool

Reputation: 63

Filling up a TCHAR array via for loop

I am trying to loop through a TCHAR array and for each loop iteration, convert the iteration number to a string and save it into the array.

The code below is what I have:

#undef  UNICODE
#define UNICODE
#undef  _UNICODE
#define _UNICODE

#include <tchar.h>
#include <string>
#include <windows.h>
#include <strsafe.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>

LRESULT CALLBACK MyTextWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
 //do stuff...

#define LINES 56
static TCHAR *abc[LINES];

for(unsigned int l = 0; l<(LINES-1); l ++){
   std::wstring s = std::to_wstring(l);
   abc[l]=TEXT(s.c_str());
}

But it gives the following error in CodeBlocks:

error: 'Ls' was not declared in this scope

I have tried reading about TCHAR and the TEXT macro. According to here https://learn.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-text the TEXT macro expects a pointer to the string which is why I tried using .c_str()

Upvotes: 0

Views: 370

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32717

The TEXT (or _T) macro is used for character strings ("string"), not variables. It will place a leading L before the argument if compiling with UNICODE (or is it _UNICODE? I never remember), so a string will become L"string".

The wstring class will return a wchar_t * pointer, which you can assign to your TCHAR * value. However, your wstring object is a local, and is destroyed at the end of the loop iteration. You'll need to either dynamically allocate space for the TCHAR * values, or allocate an array of wstring objects to hold the constructed strings that will stick around until your done using abc.

Upvotes: 1

Related Questions