Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. cstring to char * conversion

cstring to char * conversion

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    namy1
    wrote on last edited by
    #1

    Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.

    M K H N E 6 Replies Last reply
    0
    • N namy1

      Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.

      M Offline
      M Offline
      Mahesh Kulkarni
      wrote on last edited by
      #2

      for this purpose you can use strcpy(char *,CString) It will help you.

      The secret of life is not enjoyment but education through experience. - Swami Vivekananda.

      1 Reply Last reply
      0
      • N namy1

        Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #3

        You can use the operator LPCTSTR ( ), like this: CString cs = "Hello"; char * cp = (LPCTSTR) cs; Operator LPCTSTR is built in the CString class, so it's safe to use.

        Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

        N 1 Reply Last reply
        0
        • N namy1

          Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          See this[^] for converts

          _**


          **_

          WhiteSky


          1 Reply Last reply
          0
          • N namy1

            Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #5

            namy1 wrote:

            basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that

            Well if _UNICODE is defined then you will have to do some additional chores for converting to char* (Read this[^] for more info), else it's quite easy

            CString csStr = "Non unicode string";
            char *szStr = csStr.GetBuffer(0);

            AFunctionToCall( szStr );

            //Now release the buffer
            csStr.ReleaseBuffer();

            Well if you need a const char*, it's much more easier

            CString csStr = "Non unicode string";
            const char* szStr = csStr; // Calls operator LPCTSTR()
            AFunctionToCall( szStr );


            Nibu thomas A Developer Programming tips[^]  My site[^]

            1 Reply Last reply
            0
            • N namy1

              Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #6

              use CString::GetBuffer() with CString::GetLength() to copy the enitre string into the char*.


              --[:jig:]-- [My Current Status] Link2006 wrote:Let's take it outside of CP Jeremy : Please don't.I would love to see this.I'm making the popcorn already.

              Z 1 Reply Last reply
              0
              • K kakan

                You can use the operator LPCTSTR ( ), like this: CString cs = "Hello"; char * cp = (LPCTSTR) cs; Operator LPCTSTR is built in the CString class, so it's safe to use.

                Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                N Offline
                N Offline
                namy1
                wrote on last edited by
                #7

                hello "kakan":confused: I used the method u specified but its giving the following error C:\Program Files\Microsoft Visual Studio\MyProjects\interface\interfacedialog1.cpp(245) : error C2440: 'initializing' : cannot convert from 'const char *' to 'char *' Thanx:)

                K 1 Reply Last reply
                0
                • N namy1

                  Hello everyone on forum, Is it possible to convert a cstring to a char pointer if yes then how basically i have a cstring and i want to use one function which takes char * parameters now i want to pass the value that is in cstring to this function how can i do that Thanx.

                  G Offline
                  G Offline
                  gunner_uk2000
                  wrote on last edited by
                  #8

                  CString is replacable with char, ie you can put in a CString when a char[] is required. try putting in a reference to your CString ie &string

                  1 Reply Last reply
                  0
                  • N namy1

                    hello "kakan":confused: I used the method u specified but its giving the following error C:\Program Files\Microsoft Visual Studio\MyProjects\interface\interfacedialog1.cpp(245) : error C2440: 'initializing' : cannot convert from 'const char *' to 'char *' Thanx:)

                    K Offline
                    K Offline
                    kakan
                    wrote on last edited by
                    #9

                    Hello. That's right, operator LPCTSTR returns a const char * (The 'C' stands for 'const') The reason for returning a const char * is that the contents of what the const char * points to must be unaltered. So if you call a function that acually changes the content of the CString, then you have to use the CString methods GetBuffer() and ReleaseBuffer(). GetBuffer() returns a LPTSTR (a char *). But if you use GetBuffer(), then you must call ReleaseBuffer() So in your case, use GetBuffer() and ReleaseBuffer(). And check out the CString documentation at MSDN. I.e here[^]

                    Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                    1 Reply Last reply
                    0
                    • E Eytukan

                      use CString::GetBuffer() with CString::GetLength() to copy the enitre string into the char*.


                      --[:jig:]-- [My Current Status] Link2006 wrote:Let's take it outside of CP Jeremy : Please don't.I would love to see this.I'm making the popcorn already.

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #10

                      Unless a copy is actually what is desired, there is no need to copy the data for this. GetBuffer returns a valid char* that can be used until you call ReleaseBuffer (at which time the CString object will be updated with the changes you made to the buffer).

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups