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. basic string question

basic string question

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
6 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.
  • S Offline
    S Offline
    sach
    wrote on last edited by
    #1

    Hi! I have a function which returning me LPCST i.e char*. I want this value to be stored in array of 255 chars decleared as : char Arr[255]; how can i do this ?? Thx in advance

    C C Z 3 Replies Last reply
    0
    • S sach

      Hi! I have a function which returning me LPCST i.e char*. I want this value to be stored in array of 255 chars decleared as : char Arr[255]; how can i do this ?? Thx in advance

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      strncpy(Arr, lpcstr, 255); Arr[254] = 0; // just to be safe Cleek | Image Toolkits | Thumbnail maker

      1 Reply Last reply
      0
      • S sach

        Hi! I have a function which returning me LPCST i.e char*. I want this value to be stored in array of 255 chars decleared as : char Arr[255]; how can i do this ?? Thx in advance

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Returning a char array this way is tricky. Either you have to allocate the memory inside the function and free it outside (which is very ugly), or you can pass the pointer as a parameter of the function and manipulate its content. Anyway, it is much better to use the std::string (from the Standard Template Library) for which you don't need to care about memory usage.


        Cédric Moonen Software developer
        Charting control

        1 Reply Last reply
        0
        • S sach

          Hi! I have a function which returning me LPCST i.e char*. I want this value to be stored in array of 255 chars decleared as : char Arr[255]; how can i do this ?? Thx in advance

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

          If you can modify the function (that is, you control it), you can make the return value part of the function arguments:

          void getMyString(char[255]& retString)
          {
             memset(retString, 0, 255);
             // do stuff here
             // declare and initialize srcString somewhere
             strncpy(retString, srcString, 254);
          }
          

          If you don't have access, or are not allowed to change the interface, here are some alternatives:

          char buffer[255] = {0};
          strncpy(buffer, myFunction(), 254);
          
          std::string myString = myFunction();
          

          Note that both of those can be sketchy if the returning function is allocating memory on the heap and expecting you to handle cleanup (very poor design, but not too uncommon, sadly). Just keep that in mind when dealing with this kind of thing. 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

          R 1 Reply Last reply
          0
          • Z Zac Howland

            If you can modify the function (that is, you control it), you can make the return value part of the function arguments:

            void getMyString(char[255]& retString)
            {
               memset(retString, 0, 255);
               // do stuff here
               // declare and initialize srcString somewhere
               strncpy(retString, srcString, 254);
            }
            

            If you don't have access, or are not allowed to change the interface, here are some alternatives:

            char buffer[255] = {0};
            strncpy(buffer, myFunction(), 254);
            
            std::string myString = myFunction();
            

            Note that both of those can be sketchy if the returning function is allocating memory on the heap and expecting you to handle cleanup (very poor design, but not too uncommon, sadly). Just keep that in mind when dealing with this kind of thing. 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

            R Offline
            R Offline
            Roland Pibinger
            wrote on last edited by
            #5

            Zac Howland wrote:

            void getMyString(char[255]& retString)

            That won't work.

            Z 1 Reply Last reply
            0
            • R Roland Pibinger

              Zac Howland wrote:

              void getMyString(char[255]& retString)

              That won't work.

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

              You are correct. Sorry, it should have been:

              void getMyString(char retString[255])
              

              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