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. managing char arrays

managing char arrays

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelptutorial
14 Posts 5 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.
  • E Offline
    E Offline
    elephantstar
    wrote on last edited by
    #1

    How can I convert a string to an ASCII value and then store it in an element of a char array? For example, I'd like the 30 to be stored in element[18] of the array. This is what I have. Thanks! char array[20]; array = "Supernatural"; CString str = "40"; strcpy(array[18],str); error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'

    T S D 3 Replies Last reply
    0
    • E elephantstar

      How can I convert a string to an ASCII value and then store it in an element of a char array? For example, I'd like the 30 to be stored in element[18] of the array. This is what I have. Thanks! char array[20]; array = "Supernatural"; CString str = "40"; strcpy(array[18],str); error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'

      T Offline
      T Offline
      try88
      wrote on last edited by
      #2

      str.GetBuffer will work! 路漫漫其修远兮,吾将上下而求索。

      1 Reply Last reply
      0
      • E elephantstar

        How can I convert a string to an ASCII value and then store it in an element of a char array? For example, I'd like the 30 to be stored in element[18] of the array. This is what I have. Thanks! char array[20]; array = "Supernatural"; CString str = "40"; strcpy(array[18],str); error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        You can't. It's not possible to store a string as an element in a character array. Maybe what you're looking for is an array of char * ? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        T E 2 Replies Last reply
        0
        • S S Senthil Kumar

          You can't. It's not possible to store a string as an element in a character array. Maybe what you're looking for is an array of char * ? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          yes he can. CString class provides a LPTSTR cast operator... so using strcpy() is possible as it uses this way 2 char arrays...


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          S 1 Reply Last reply
          0
          • S S Senthil Kumar

            You can't. It's not possible to store a string as an element in a character array. Maybe what you're looking for is an array of char * ? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            E Offline
            E Offline
            elephantstar
            wrote on last edited by
            #5

            Yes, I do have an array of characters. I need to convert the ASCII value which is currently a string value to an ASCII character. Then store that ASCII character into the 18th element of the array. How do I go about doing that?

            D 1 Reply Last reply
            0
            • E elephantstar

              How can I convert a string to an ASCII value and then store it in an element of a char array? For example, I'd like the 30 to be stored in element[18] of the array. This is what I have. Thanks! char array[20]; array = "Supernatural"; CString str = "40"; strcpy(array[18],str); error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              elephantstar wrote: array = "Supernatural"; What is this, other than a syntax error? If you want array to contain the string literal, use either of:

              char array[20] = "Supernatural";
              ...
              char array[20];
              strcpy(array, "Supernatural");

              elephantstar wrote: strcpy(array[18],str); The error message tells you exactly what the problem is. array[18] is a char, but the first parameter of strcpy() is supposed to be a char*. Why are you using char in such a fashion? If this is an MFC application, use CString. Mixing the two types is seldom necessary.


              "One must learn from the bite of the fire to leave it alone." - Native American Proverb

              E 1 Reply Last reply
              0
              • E elephantstar

                Yes, I do have an array of characters. I need to convert the ASCII value which is currently a string value to an ASCII character. Then store that ASCII character into the 18th element of the array. How do I go about doing that?

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Are you talking about this:

                CString str = "40";
                array[18] = atoi(str);


                "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                1 Reply Last reply
                0
                • T toxcct

                  yes he can. CString class provides a LPTSTR cast operator... so using strcpy() is possible as it uses this way 2 char arrays...


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc]

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #8

                  No he can't :). It's not possible to store a string as an element in an array of characters. From what I understood, the OP wants to do this

                  char arr[20] = "Senthil";
                  arr[5] = "Kumar";

                  which isn't logically possible. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                  T 1 Reply Last reply
                  0
                  • D David Crow

                    elephantstar wrote: array = "Supernatural"; What is this, other than a syntax error? If you want array to contain the string literal, use either of:

                    char array[20] = "Supernatural";
                    ...
                    char array[20];
                    strcpy(array, "Supernatural");

                    elephantstar wrote: strcpy(array[18],str); The error message tells you exactly what the problem is. array[18] is a char, but the first parameter of strcpy() is supposed to be a char*. Why are you using char in such a fashion? If this is an MFC application, use CString. Mixing the two types is seldom necessary.


                    "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                    E Offline
                    E Offline
                    elephantstar
                    wrote on last edited by
                    #9

                    I want to store the value of CString str into the array. vtValue.ChangeType(VT_BSTR); str = (LPCSTR)((_bstr_t)vtValue.bstrVal); Then I want to store the exact same data type into array[18]. Yes, it is an MFC application. I'm using char so that I can manipulate the data in each element easily.

                    D 1 Reply Last reply
                    0
                    • E elephantstar

                      I want to store the value of CString str into the array. vtValue.ChangeType(VT_BSTR); str = (LPCSTR)((_bstr_t)vtValue.bstrVal); Then I want to store the exact same data type into array[18]. Yes, it is an MFC application. I'm using char so that I can manipulate the data in each element easily.

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      elephantstar wrote: vtValue.ChangeType(VT_BSTR); str = (LPCSTR)((_bstr_t)vtValue.bstrVal); What's with all of the BSTR and VARIANT stuff? Is this part of the original problem? elephantstar wrote: I'm using char so that I can manipulate the data in each element easily. CString objects can be manipulated just as easily.


                      "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                      E 1 Reply Last reply
                      0
                      • D David Crow

                        elephantstar wrote: vtValue.ChangeType(VT_BSTR); str = (LPCSTR)((_bstr_t)vtValue.bstrVal); What's with all of the BSTR and VARIANT stuff? Is this part of the original problem? elephantstar wrote: I'm using char so that I can manipulate the data in each element easily. CString objects can be manipulated just as easily.


                        "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                        E Offline
                        E Offline
                        elephantstar
                        wrote on last edited by
                        #11

                        Sorry, I should have included the rest of the code. I'm using ADO to query some values. CString str, str2; _variant_t vtValue; vtValue = t_Rec->Fields->GetItem("NAME")->GetValue(); vtValue.ChangeType(VT_BSTR); str = (LPCSTR)((_bstr_t)vtValue.bstrVal); Instead of using char array[20], you're suggesting I just work with CString then. But if I do use CString, the following code fails. What am I doing wrong? Thanks.

                        vtValue = t_Rec->Fields->GetItem("NUMBER")->GetValue();
                        vtValue.ChangeType(VT_BSTR);
                        str2 = (LPTSTR)((_bstr_t)vtValue.bstrVal);
                        char x;
                        x = atoi(str2);
                        str.GetAt(18, str2); ///Debug Assertion Failed

                        D 1 Reply Last reply
                        0
                        • E elephantstar

                          Sorry, I should have included the rest of the code. I'm using ADO to query some values. CString str, str2; _variant_t vtValue; vtValue = t_Rec->Fields->GetItem("NAME")->GetValue(); vtValue.ChangeType(VT_BSTR); str = (LPCSTR)((_bstr_t)vtValue.bstrVal); Instead of using char array[20], you're suggesting I just work with CString then. But if I do use CString, the following code fails. What am I doing wrong? Thanks.

                          vtValue = t_Rec->Fields->GetItem("NUMBER")->GetValue();
                          vtValue.ChangeType(VT_BSTR);
                          str2 = (LPTSTR)((_bstr_t)vtValue.bstrVal);
                          char x;
                          x = atoi(str2);
                          str.GetAt(18, str2); ///Debug Assertion Failed

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          elephantstar wrote: str.GetAt(18, str2); ///Debug Assertion Failed This must be .Net as the CString::GetAt() method that comes with VC++ v6 only takes one parameter. In any case, what line of what file is firing the assertion?


                          "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                          E 1 Reply Last reply
                          0
                          • D David Crow

                            elephantstar wrote: str.GetAt(18, str2); ///Debug Assertion Failed This must be .Net as the CString::GetAt() method that comes with VC++ v6 only takes one parameter. In any case, what line of what file is firing the assertion?


                            "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                            E Offline
                            E Offline
                            elephantstar
                            wrote on last edited by
                            #13

                            Nevermind. I went back to using char and it works just fine. Thanks Dave! Your help is always appreciated.

                            1 Reply Last reply
                            0
                            • S S Senthil Kumar

                              No he can't :). It's not possible to store a string as an element in an array of characters. From what I understood, the OP wants to do this

                              char arr[20] = "Senthil";
                              arr[5] = "Kumar";

                              which isn't logically possible. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                              T Offline
                              T Offline
                              toxcct
                              wrote on last edited by
                              #14

                              oh yes, i did this it like this. i saw (arr + 5) = "Kumar"; which would mean writing from the 5th position in the array. but ok, you're right, i appology.


                              TOXCCT >>> GEII power
                              [toxcct][VisualCalc]

                              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