Thread: I give up - TCHAR, char - initializing/printing

  1. #16
    Registered User
    Join Date
    Feb 2012
    Posts
    44
    I always get a compiler error when I try what you did on #7. I always have to use s t r c p y / _ t c s c p y.

    Can anyone show how to use squiggly brackets/braces to assign to T C H A R ?

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Do you understand regular C-strings and arrays? You don't use braces to assign to a TCHAR. You can use it to *initialize* an array of TCHAR, just like any other array, but you can't use it to *assign* to a TCHAR. You *must* use _tcscpy.

    Show us exactly what you're trying to do that's failing.

  3. #18
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Since you are just learning C, you really should forget about this TCHAR stuff.

    Forget MSVC. Use CodeBlocks (with gcc) and spend a year learning the basics.

    Then you'll be able to figure out the TCHAR thing yourself. At this point it's a waste of time.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #19
    Registered User
    Join Date
    Feb 2012
    Posts
    44
    Quote Originally Posted by rags_to_riches View Post
    Do you understand regular C-strings and arrays? You don't use braces to assign to a TCHAR. You can use it to *initialize* an array of TCHAR, just like any other array, but you can't use it to *assign* to a TCHAR. You *must* use _tcscpy.

    Show us exactly what you're trying to do that's failing.
    Why is everyone not following.

    The original question: please provide the many ways one can assign data (printable, hex, decimal) to a TCHAR (whether single-byte TCHAR or array) directly (and not via for loop or from any other source).

    My program is already 1500 lines of code and will be around 5000 when complete. I have all my parsing routines complete (using TCHAR). I do NOT understand the syntax of assigning data TO a TCHAR other than simple 'x' or _T("longstring"). As I said, I had trouble assigning a 0xnnnnnnn value to a TCHAR. With the UNICODE compile option on (default), does TCHAR mandate a 2-byte hex field? Will it pad a 1-byte with 0x00?

    I understand all the types of string data for Windows. But I do NOT understand the SYNTAX of how to do "tricky" (actually normal) things to them when it comes to assignment.

  5. #20
    Registered User
    Join Date
    Feb 2012
    Posts
    44
    Quote Originally Posted by oogabooga View Post
    Since you are just learning C, you really should forget about this TCHAR stuff.

    Forget MSVC. Use CodeBlocks (with gcc) and spend a year learning the basics.

    Then you'll be able to figure out the TCHAR thing yourself. At this point it's a waste of time.
    I already understand the internals of TCHARS. I do NOT fully understand the COMPILER SYNTAX of how to assign all the flavors of bits one can put in a TCHAR. Hence, my original post.

    You don't need a year for the basics. It's only been a few weeks and the hardest part of the program is complete (all the parsing).

  6. #21
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by jlewand View Post
    As I said, I had trouble assigning a 0xnnnnnnn value to a TCHAR. With the UNICODE compile option on (default), does TCHAR mandate a 2-byte hex field? Will it pad a 1-byte with 0x00?
    Because there are no other ways, you don't do that. TCHAR isn't a swiss army knife. If you want a type to hold a number, use 'int' or one of the myriad of other fixed size types to accommodate it. No, they wont make your program consume any more or less memory, and no they won't make your program any slower or faster.

  7. #22
    Registered User
    Join Date
    Feb 2012
    Posts
    44
    Quote Originally Posted by adeyblue View Post
    Because there are no other ways, you don't do that. TCHAR isn't a swiss army knife. If you want a type to hold a number, use 'int' or one of the myriad of other fixed size types to accommodate it. No, they wont make your program consume any more or less memory, and no they won't make your program any slower or faster.
    I was using a single TCHAR field as holding bit settings for a function. I suppose I could use an int but it's counterintuitive to an assembler programmer to use a word/register to hold bit settings. I got the 0xnnn assignment to work, but always have to printf %x to see if it got stored as expected.....

  8. #23
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I was using a single TCHAR field as holding bit settings for a function.
    That's not what TCHAR is for. If you just want a type to store at least 8 bits, then use "unsigned char", or Window's BYTE type.

    For hex values in character and string literals, you can use the '\x' escape sequence followed by a hex number. In a TCHAR literal, there should never be more than 2 hex digits. Eg: _T('\xff') is a single TCHAR with a value of 0xff. But this should never be needed in the first place.

    I would be interested in knowing if your code compiles and runs correctly when you toggle the "Character Set" project-property in Visual Studio.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with initializing a char array
    By csepraveenkumar in forum C Programming
    Replies: 10
    Last Post: 03-22-2011, 01:25 AM
  2. TCHAR * to unsinged char []
    By simly01 in forum C Programming
    Replies: 5
    Last Post: 11-14-2004, 03:05 PM
  3. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 05:19 PM
  4. tchar or just char
    By stallion in forum Windows Programming
    Replies: 5
    Last Post: 01-23-2003, 01:44 PM
  5. initializing char arrays to null
    By swiss powder in forum C++ Programming
    Replies: 6
    Last Post: 02-28-2002, 02:56 PM