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. 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *'

'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *'

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelpquestion
8 Posts 4 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.
  • S Offline
    S Offline
    Swap9
    wrote on last edited by
    #1

    I am trying to enable an application for unicode. //char c_buffer[ARRAY]; TCHAR c_buffer[ARRAY]; above are the changes I made into code. Which breaks at this code in the .cpp file sprintf(c_buffer, "%d", pb->i_id); abc.cpp: error C2664: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Why so ? I have added TCHAR.h and strsafe.h

    _ CPalliniC 2 Replies Last reply
    0
    • S Swap9

      I am trying to enable an application for unicode. //char c_buffer[ARRAY]; TCHAR c_buffer[ARRAY]; above are the changes I made into code. Which breaks at this code in the .cpp file sprintf(c_buffer, "%d", pb->i_id); abc.cpp: error C2664: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Why so ? I have added TCHAR.h and strsafe.h

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Use wsprintf -

      wsprintf(c_buffer, _T("%d"), pb->i_id);

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      S 2 Replies Last reply
      0
      • S Swap9

        I am trying to enable an application for unicode. //char c_buffer[ARRAY]; TCHAR c_buffer[ARRAY]; above are the changes I made into code. Which breaks at this code in the .cpp file sprintf(c_buffer, "%d", pb->i_id); abc.cpp: error C2664: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Why so ? I have added TCHAR.h and strsafe.h

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Quote:

        sprintf(c_buffer, "%d", pb->i_id);

        change to

        _stprintf(c_buffer, _T("%d"), pb->i_id);

        see sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l[^]

        THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

        In testa che avete, signor di Ceprano?

        S 1 Reply Last reply
        0
        • _ _Superman_

          Use wsprintf -

          wsprintf(c_buffer, _T("%d"), pb->i_id);

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

          Polymorphism in C

          S Offline
          S Offline
          Swap9
          wrote on last edited by
          #4

          Worked ! great... thanks :)

          1 Reply Last reply
          0
          • CPalliniC CPallini

            Quote:

            sprintf(c_buffer, "%d", pb->i_id);

            change to

            _stprintf(c_buffer, _T("%d"), pb->i_id);

            see sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l[^]

            THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

            S Offline
            S Offline
            Swap9
            wrote on last edited by
            #5

            However, the swprintf function, which is a VC++ library function is the correct one to use.
            The wsprintf function is part of some windows API, and does not support all the formatting options. For example wsprintf does not support the "*" width specification.

            Thanks !

            L 1 Reply Last reply
            0
            • S Swap9

              However, the swprintf function, which is a VC++ library function is the correct one to use.
              The wsprintf function is part of some windows API, and does not support all the formatting options. For example wsprintf does not support the "*" width specification.

              Thanks !

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              If you are using TCHAR to define your data then you must use the routines with the _st prefix as described in http://msdn.microsoft.com/en-us/library/ybk95axf.aspx[^]/ This allows the compiler to generate code for ASCII or Unicode depending on your project settings. If your program is only ever going to handle Unicode character data then you should use WCHAR and the PW prefixed pointers.

              1 Reply Last reply
              0
              • _ _Superman_

                Use wsprintf -

                wsprintf(c_buffer, _T("%d"), pb->i_id);

                «_Superman_»  _I love work. It gives me something to do between weekends.

                _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                Polymorphism in C

                S Offline
                S Offline
                Swap9
                wrote on last edited by
                #7

                Hello superman...just trying my luck by asking this to you... problem piece of code - if(Function(parm1, parm2, parm3)==0){ error - cannot convert parameter 3 from 'char [1024]' to 'wchar_t *' Function is expecting wchar_t but its getting char[1024] from some where. I changed the function signature to accept wchar_t type in header file. objective is to make the whole code UNICODE enabled. I have added all the tchar.h, wchar.h fiels param 3 has been defined (modified) as wchar_t in all the connected files too. I cant debug yet. Pls help how can i get rid of this error. I am working in vs2010.I counter checked it about five times and gave the search in solution to find if parm3 is still 'char' somewhere else. I am seriously doubting typecasting the wchar_t parm3[] before passing it function i think. I went to function declaration and changed it to char* to wchar_t* too. I doubt that might not be straight forward. there should be some changes.

                _ 1 Reply Last reply
                0
                • S Swap9

                  Hello superman...just trying my luck by asking this to you... problem piece of code - if(Function(parm1, parm2, parm3)==0){ error - cannot convert parameter 3 from 'char [1024]' to 'wchar_t *' Function is expecting wchar_t but its getting char[1024] from some where. I changed the function signature to accept wchar_t type in header file. objective is to make the whole code UNICODE enabled. I have added all the tchar.h, wchar.h fiels param 3 has been defined (modified) as wchar_t in all the connected files too. I cant debug yet. Pls help how can i get rid of this error. I am working in vs2010.I counter checked it about five times and gave the search in solution to find if parm3 is still 'char' somewhere else. I am seriously doubting typecasting the wchar_t parm3[] before passing it function i think. I went to function declaration and changed it to char* to wchar_t* too. I doubt that might not be straight forward. there should be some changes.

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #8

                  To support UNICODE, you need to use wchar_t instead of char. You also need to use the string functions that work on wchar_t like wcslen, wcscat etc. While defining string literals use the L prefix - L"Hello World" If you have a string that is already encoded as ASCII, you need to convert it to UNICODE. Simply typecasting will not work. You can use the MultiByteToWideChar[^] API to do the conversion.

                  «_Superman_»  _I love work. It gives me something to do between weekends.

                  _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                  Polymorphism in C

                  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