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. Concatenation of two LPSTR

Concatenation of two LPSTR

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 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.
  • _ Offline
    _ Offline
    __Cerb
    wrote on last edited by
    #1

    Well, I have my string, let's say LPSTR myString = "up?"; I'd want to add " Whats " in front of it, just like LPSTR myString = "up?"; myString = "Whats " + myString; I get an error. Then I tried doing: LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString); This also doesn't work. Any help please? Thanks ~Mike

    A A M C 4 Replies Last reply
    0
    • _ __Cerb

      Well, I have my string, let's say LPSTR myString = "up?"; I'd want to add " Whats " in front of it, just like LPSTR myString = "up?"; myString = "Whats " + myString; I get an error. Then I tried doing: LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString); This also doesn't work. Any help please? Thanks ~Mike

      A Offline
      A Offline
      antlers
      wrote on last edited by
      #2

      You have to understand that an LPSTR is a very primitive thing: a pointer to an array of characters that is supposed to have a \0 on the end. You can do operations with LPSTR (look at the strcat function) but you have to make sure that you have allocated another array to hold the result that is large enough. Manipulating strings this way has been done for years in C and has led to innumerable buffer overflow errors. You should never do it in C++. You should use the string classes (std::string, std::wstring for wide characters) that are in the standard library, which take care of buffer allocation (and many other useful things) for you. You can concatenate std::string with the overloaded + operator and get just what you expect.

      1 Reply Last reply
      0
      • _ __Cerb

        Well, I have my string, let's say LPSTR myString = "up?"; I'd want to add " Whats " in front of it, just like LPSTR myString = "up?"; myString = "Whats " + myString; I get an error. Then I tried doing: LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString); This also doesn't work. Any help please? Thanks ~Mike

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        this seemed to work: #include #include #include LPSTR concat(LPSTR s1, LPSTR s2) { char* str = new char[strlen(s1) + strlen(s2) + 1]; LPSTR final_str = str; strcpy(str, s1); strcat(str, s2); return final_str; } int main() { LPSTR s1 = "this is a"; LPSTR s2 = " test."; cout << concat(s1, s2) << endl; return 0; }

        1 Reply Last reply
        0
        • _ __Cerb

          Well, I have my string, let's say LPSTR myString = "up?"; I'd want to add " Whats " in front of it, just like LPSTR myString = "up?"; myString = "Whats " + myString; I get an error. Then I tried doing: LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString); This also doesn't work. Any help please? Thanks ~Mike

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

          Quick answer: C-style strings don't work like that. Long answer: See the above replies for some code that does what you want, although you must learn how C-style strings work in order to use the strcat/strcpy functions properly. The std::string class is much easier to learn and use if you don't absolutely need a C-style string. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Actual sign at the laundromat I go to: "No tinting or dying."

          1 Reply Last reply
          0
          • _ __Cerb

            Well, I have my string, let's say LPSTR myString = "up?"; I'd want to add " Whats " in front of it, just like LPSTR myString = "up?"; myString = "Whats " + myString; I get an error. Then I tried doing: LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString); This also doesn't work. Any help please? Thanks ~Mike

            C Offline
            C Offline
            chpsoft
            wrote on last edited by
            #5

            it is wrong certainly.because your "myString" points to a static data area,which can not be changed.so ,your "myString = "Whats " + myString" is wrong.you must do so: char str[10]="up?"; char mystring[20]; sprintf(mystring,"Whats %s",str);

            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