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. How to use strcpy to modify char** ?

How to use strcpy to modify char** ?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialquestion
7 Posts 6 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.
  • J Offline
    J Offline
    Joe Smith IX
    wrote on last edited by
    #1

    The strcpy below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.

    char **pArrayLabel = NULL; // I have to declare it this way, no vector.
    // (It will be pass to another function later)

    int size=12;
    pArrayLabel = new char*[size];

    CString szLabel0 = "Januari";
    strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength()));

    M P T P 4 Replies Last reply
    0
    • J Joe Smith IX

      The strcpy below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.

      char **pArrayLabel = NULL; // I have to declare it this way, no vector.
      // (It will be pass to another function later)

      int size=12;
      pArrayLabel = new char*[size];

      CString szLabel0 = "Januari";
      strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength()));

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      You're copying into an uninitialized pointer. You need to allocate a char buffer for each of the 12 char*s

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

      1 Reply Last reply
      0
      • J Joe Smith IX

        The strcpy below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.

        char **pArrayLabel = NULL; // I have to declare it this way, no vector.
        // (It will be pass to another function later)

        int size=12;
        pArrayLabel = new char*[size];

        CString szLabel0 = "Januari";
        strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength()));

        P Offline
        P Offline
        Peter Weyzen
        wrote on last edited by
        #3

        I would do this: char *pArrayLabel = NULL; int size=12; pArrayLabel = new char[size]; CString szLabel0 = "Januari"; strcpy(pArrayLabel,(char *)szLabel0.GetBuffer(szLabel0.GetLength())); And when you pass it to the other function, pass it as "&pArrayLabel"

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc.](http://www.soonr.com)

        1 Reply Last reply
        0
        • J Joe Smith IX

          The strcpy below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.

          char **pArrayLabel = NULL; // I have to declare it this way, no vector.
          // (It will be pass to another function later)

          int size=12;
          pArrayLabel = new char*[size];

          CString szLabel0 = "Januari";
          strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength()));

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

          once again, somebody is using CString::GetBuffer() when it's definitely not needed. Call GetBuffer() only if you need to modify the CString object directly by writing inside its internal buffer. and this call to GetBuffer() requires a call to ReleaseBuffer() (which you don't do here). if you only need to get the string contained within the CString, then you must use the cast operator (LPCTSTR) and that's it. and for your problem, as that's been stated, you're assigning a NULL pointer. modify your code into this. ans at last, as i'm on it, please be careful with your variables names (generally, a variable starting with sz or psz is declared being a C-Style string)

          const int iSize = 12;
          TCHAR *pArrayLabel = ::new TCHAR[iSize];
          CString strLabel = _T("Januari");
          ::_tcscpy_s(pArrayLabel[0], (LPCTSTR)szLabel0);


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          M M 2 Replies Last reply
          0
          • T toxcct

            once again, somebody is using CString::GetBuffer() when it's definitely not needed. Call GetBuffer() only if you need to modify the CString object directly by writing inside its internal buffer. and this call to GetBuffer() requires a call to ReleaseBuffer() (which you don't do here). if you only need to get the string contained within the CString, then you must use the cast operator (LPCTSTR) and that's it. and for your problem, as that's been stated, you're assigning a NULL pointer. modify your code into this. ans at last, as i'm on it, please be careful with your variables names (generally, a variable starting with sz or psz is declared being a C-Style string)

            const int iSize = 12;
            TCHAR *pArrayLabel = ::new TCHAR[iSize];
            CString strLabel = _T("Januari");
            ::_tcscpy_s(pArrayLabel[0], (LPCTSTR)szLabel0);


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            toxcct wrote:

            once again, somebody is using CString::GetBuffer() when it's definitely not needed.

            :laugh: Yeah, they should charge a toll $$$ to use CString::GetBuffer() and CWnd::PreTranslateMessage() to help people consider alternative solutions :)

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            1 Reply Last reply
            0
            • T toxcct

              once again, somebody is using CString::GetBuffer() when it's definitely not needed. Call GetBuffer() only if you need to modify the CString object directly by writing inside its internal buffer. and this call to GetBuffer() requires a call to ReleaseBuffer() (which you don't do here). if you only need to get the string contained within the CString, then you must use the cast operator (LPCTSTR) and that's it. and for your problem, as that's been stated, you're assigning a NULL pointer. modify your code into this. ans at last, as i'm on it, please be careful with your variables names (generally, a variable starting with sz or psz is declared being a C-Style string)

              const int iSize = 12;
              TCHAR *pArrayLabel = ::new TCHAR[iSize];
              CString strLabel = _T("Januari");
              ::_tcscpy_s(pArrayLabel[0], (LPCTSTR)szLabel0);


              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              The (LPCTSTR) cast is superfluous there too, because the compiler will automatically call it when it sees that the _tcscpy() parameter is an LPCTSTR.

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

              1 Reply Last reply
              0
              • J Joe Smith IX

                The strcpy below throw 'Access Violation'. Can anybody show me how to do it correctly? Thanks.

                char **pArrayLabel = NULL; // I have to declare it this way, no vector.
                // (It will be pass to another function later)

                int size=12;
                pArrayLabel = new char*[size];

                CString szLabel0 = "Januari";
                strcpy(pArrayLabel[0],(char *)szLabel0.GetBuffer(szLabel0.GetLength()));

                P Offline
                P Offline
                prasad_som
                wrote on last edited by
                #7

                You already got enough information from other replies. If you are using visual studio 2005, consider using secured version of these CRT function. Refer Security enhancement to CRT[^].


                Prasad MS MVP -  VC++

                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