MrsTickle

i am new to c++ so i think this is a silly question, but i'm a little confused.

i am populating a TCHAR string.

this works :

TCHAR StrData[] = _T("TextInformation");

i want to declare StrData globally, so have used

__declspec(selectany) TCHAR StrData[];

in a header file and include it where needed.

but how do i now populate the StrData field

StrData[] = _T("TextInformation"); //fails with a syntax error ]

StrData[15] = _T("TextInformation");//Error 1 error C2440: '=' : cannot convert from 'const wchar_t [11]' to 'TCHAR'



Re: Visual C++ Language Populating globally defined tchar field

Tomay

The first command fails because you can't assign one array to anther one, you need to use some function like strncpy, that works with TCHAR.
The second instruction fails, because you can't assign an array of TCHARs to one TCHAR location, something like StrData[15] = _T( 'T' ); should probably work.




Re: Visual C++ Language Populating globally defined tchar field

Simple Samples

The documentation of __declspec(selectany) has the following example:

Code Snippet
__declspec(selectany) int x1=1;

So if you use that syntax, then it is simply a matter of intiallizing the string in the manner you are familiar with instead of initializing the int as in the example.






Re: Visual C++ Language Populating globally defined tchar field

MrsTickle

Thanks for that. I'm still learning c++ and it so infuriating.

if i use ' rather than " that compiles ok but it doesn't populate the string correctly. (appears empty)

i've tried strncpy but i just get a different translation error. aaahhhh !! ! ! ! ! !





Re: Visual C++ Language Populating globally defined tchar field

MrsTickle

Simple Samples wrote:

The documentation of __declspec(selectany) has the following example:

Code Snippet
__declspec(selectany) int x1=1;

So if you use that syntax, then it is simply a matter of intiallizing the string in the manner you are familiar with instead of initializing the int as in the example.

OK, but how would i change the value after initialisation





Re: Visual C++ Language Populating globally defined tchar field

Simple Samples

MrsTickle wrote:

Thanks for that. I'm still learning c++ and it so infuriating.

if i use ' rather than " that compiles ok but it doesn't populate the string correctly. (appears empty)

i've tried strncpy but i just get a different translation error. aaahhhh !! ! ! ! ! !

Yes strings can be confusing. If you can use a std:Tongue Tiedtring instead of TCHAR then that will be easier. Also, a good book about C++ will help.

Show us the code you are using that is not working. If you are familiar with TCHAR strings then you should not have any problem with this.

Note that strncpy is an ANSI function and your program is probably using Unicode. That is a likely problem. You need to use generic text mapping macros. TCHAR is a generic text mapping macro. Generic text mapping macros are not described in most books about C++. Look at the documentation of strncpy to see what the equivalent generic text mapping macro is. Also be sure to use the "T" macro for string literals (I see that you are doing that).






Re: Visual C++ Language Populating globally defined tchar field

Simple Samples

MrsTickle wrote:
OK, but how would i change the value after initialisation

The same as for strings declared locally.






Re: Visual C++ Language Populating globally defined tchar field

Holger Grund

I think the OP's point is that array initialization and assignment is different:

char x[]="Snoopy"; // OK

x = "Snoopy"; // error - can't assign to an array

I'm not sure why the limitation exists, but that's the way it is. Of course, there are ways around it (strcpy, memcpy et al. or wrapping the array in a structure and copying that), but it's usually a good idea to use a C++ wrapper like std:Tongue Tiedtring, CString etc. And since these aren't arrays and have overloaded assignment operators, assignment will usually work in much the same way as initialization.

All that being said, that's pretty basic stuff and a C++ book would probably help.

-hg