Convert CString to char for use in Switch

I know that I can't use a CString in a switch statement, but I only need to do a comparison against the first letter of the string.  I can not figure out how to take the letter from the CString and put it into a char so I can use it in switch statement.

This seems like a simple thing to do, but all searches on CString and char pull up char pointers and that is what I am trying to get away from.

Thanks in advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>> I can not figure out how to take the letter from the CString
>>and put it into a char so I can use it in switch statement


CString str ( "test");
char* pc = (LPCTSTR) str;

switch ( *pc) {

case 'a':




case 't':

To elaborate on the above - the docs explain 'operator LPCTSTR()' as follows:


CString::operator LPCTSTR
operator LPCTSTR ( ) const;

Return Value

A character pointer to the string’s data.


This useful casting operator provides an efficient method to access the null-terminated C string contained in a CString object. No characters are copied; only a pointer is returned. Be careful with this operator. If you change a CString object after you have obtained the character pointer, you may cause a reallocation of memory that invalidates the pointer.


So, if you have acces to the contents through a char*, you can dereference the pointer using '*pc'  to access the first character.
APlusComp247Author Commented:
This is part of the code that I am use

                szFileName = data.cFileName;
      char* cLetter = (LPCTSTR) szFirstLetter;
      switch (*cLetter)
      case 'a':
           m_nCount[1] ++;
             m_nCount[0] ++;

I get the following error.

error C2440: 'initializing' : cannot convert from 'const char *' to 'char *'
        Conversion loses qualifiers

Thanks for the prompt reply
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Ooops, make that read

const char* cLetter = (LPCTSTR) szFirstLetter;

and it will work. Since you are not going to manipulate the char*, this is the easiest solution.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APlusComp247Author Commented:
That works great!!!!   Thank you so much for you quick responces!
Better would be :
char cLetter = sTextYouWantFirstLetter.GetAt(0);
APlusComp247Author Commented:
Thanks a lot for the post.  I am by no means a C++/MFC pro and really appreciate all the little shortcuts that I can pick up.  I really thought that there should be an easier way to do that, and sure enough you showed me.

Thanks again!
I'd use

CString str = "SomeString";

switch (str[0]) //0 so we point to the first char
     case 'S':
          //other stuff
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.