Thread: String types and conversions (TCHAR)

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    String types and conversions (TCHAR)

    Code:
    void printText(HWND hWnd, TCHAR txt[], RECT& rc)
    {
    	PAINTSTRUCT pntS;
    
    	HDC hdc = BeginPaint(hWnd, &pntS);
    	TCHAR s[] = _T(txt);
    	TextOut(hdc, rc.left, rc.top, s, (int)_tcslen(s));
    	EndPaint(hWnd, &ps);
    }
    Got an error...

    Code:
    error C2440: 'initializing' : cannot convert from 'TCHAR []' to 'TCHAR []'
    It's complaining about
    Code:
    TCHAR s[] = _T(txt);
    ??

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    _T() is macro that takes a string-literal only - something in quotation marks.
    Just call _tcslen(txt) directly.

    gg

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Codeplug View Post
    _T() is macro that takes a string-literal only - something in quotation marks.
    Just call _tcslen(txt) directly.

    gg
    Isn't _tcslen just to get the length of the string?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What Codeplug is referring to is that you don't need to create another temporary variable, because txt is perfecetly fine to use where you are using s.

    What the compiler is complaining about is actually that you are trying to copy one array into another. If you are going to copy a string, then you should:
    1. not use the _T macro, as that's only for string literals (things that have quotes around it).
    2. use _tcscpy() to copy the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! About Type Conversions
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 05-28-2006, 09:24 PM
  2. Replies: 5
    Last Post: 06-03-2002, 08:47 AM