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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. newbie question about strcpy and pointers

newbie question about strcpy and pointers

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • N Offline
    N Offline
    ns
    wrote on last edited by
    #1

    I have a function which is supposed to fill in abc: MyFunc(char* abc) { char* val = "aaa"; strcpy(abc,val); } Why do I have to strcpy? If I just do: { char* val = "aaa"; abc = val; } then there is garbage in abc when it returns. WHat exactly is happening if you do it my second way? Why doesnt it work?

    P C S P 4 Replies Last reply
    0
    • N ns

      I have a function which is supposed to fill in abc: MyFunc(char* abc) { char* val = "aaa"; strcpy(abc,val); } Why do I have to strcpy? If I just do: { char* val = "aaa"; abc = val; } then there is garbage in abc when it returns. WHat exactly is happening if you do it my second way? Why doesnt it work?

      P Offline
      P Offline
      Peter Liddle
      wrote on last edited by
      #2

      You can't do that and teh reason why is that in your second implementation you are trying to set the value of the pointer and not of the character array. And you need to actaully copy the memory containg the "AAA" string to the memory that the pointer points to. Hope this helps Peter

      1 Reply Last reply
      0
      • N ns

        I have a function which is supposed to fill in abc: MyFunc(char* abc) { char* val = "aaa"; strcpy(abc,val); } Why do I have to strcpy? If I just do: { char* val = "aaa"; abc = val; } then there is garbage in abc when it returns. WHat exactly is happening if you do it my second way? Why doesnt it work?

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

        in the second, you are assigning the address of a local variable (val is local to the function) to a non-local variable. a local address is meaningless when used outside the function. -c


        To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses.
           /. #3848917

        Fractals!

        N 1 Reply Last reply
        0
        • C Chris Losinger

          in the second, you are assigning the address of a local variable (val is local to the function) to a non-local variable. a local address is meaningless when used outside the function. -c


          To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses.
             /. #3848917

          Fractals!

          N Offline
          N Offline
          ns
          wrote on last edited by
          #4

          Many thanks! I think I get it. So if I had func(int & abc) { int a=9; abc=a; } then when called like int zz; func(zz); zz will be equal to 9. Is this correct? And we didnt have to copy anything because we're not dealing with addresses, but values. Thanks again, ns

          RaviBeeR 1 Reply Last reply
          0
          • N ns

            I have a function which is supposed to fill in abc: MyFunc(char* abc) { char* val = "aaa"; strcpy(abc,val); } Why do I have to strcpy? If I just do: { char* val = "aaa"; abc = val; } then there is garbage in abc when it returns. WHat exactly is happening if you do it my second way? Why doesnt it work?

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

            Function scope would be a good place to start. You wrote the following function... ns wrote: MyFunc(char* abc) { char* val = "aaa"; abc = val; } It has to do with both pointers and with memory allocation. First by reassigning pointers such as: ns wrote: abc = val you are now pointing the incoming character pointer to the same memory as 'val'. Remember though, there are two things going on here. First: Function scope does not allow the changes to abc to be reflected to the caller. The actual value that is passed in as 'abc' is not stored at the same location as the orginal 'abc'. The following code trys to demonstrate the timing and values of the pointers. Main { char* abc = "Home"; // abc = memory addr = 0x00000a, *abc (contents of abc or memory 0x0000a is "H") MyFunc(abc); // FUNCTION EMBEDDED TO DEMOSTRATE TIMING MyFunc(char* abc) { char* val = "aaa"; // val = memory addr = 0x01000, *val (contents of val) is "a" // abc = memory addr = 0x0000a, *val (contes of abc) is "H" abc = val; // val = memory addr = 0x01000, *val (contents of val) is "a" // abc = memory addr = 0x01000, *val (contes of abc) is "a" } // abc is reset by the stack back to the original. This is function scope. // abc = memory addr = 0x00000a, *abc (contents of abc or memory 0x0000a is "H") } Second even if you corrected this to pass a pointer to a pointer: (pass in a pointer to the pointer where the memory for the string is) pabc->abc->"The string" pabc = 0x02000 which is the memory addr of abc abc = 0x0000a which is the memory where the string resides. MyFunc(char** pabc) { char* val = "aaa"; *pabc = val; } It changes to pabc->val->"aaa" and pabc->abc->"aaa" while still in the function call. However remember that the function releases its stack values when it exits so we cannot guarantee that someone else will not write to that memory. So the memory that was at the orginal abc pointer is still allocated, but the memory at val or the new abc is released and can be unexpected garbage. When you use: ns wrote: strcpy(abc,val) you are physically reassigning memory from one place to another. You are retaining the orginal area in memory for each string and moving the contents. A very simple rewrite of strcpy is this: strcpy(char* a, char* b) { // look for an ending null termination while (*b != '0') {

            1 Reply Last reply
            0
            • N ns

              Many thanks! I think I get it. So if I had func(int & abc) { int a=9; abc=a; } then when called like int zz; func(zz); zz will be equal to 9. Is this correct? And we didnt have to copy anything because we're not dealing with addresses, but values. Thanks again, ns

              RaviBeeR Offline
              RaviBeeR Offline
              RaviBee
              wrote on last edited by
              #6

              Yep, that's correct. In your previous strcpy() question, you must ensure that the target string has enough storage allocated. For example, this will fail:

              char strMyString [8];
              myFunc (strMyString);
              ...
              void myFunc (char* pString)
              {
              strcpy (pString, "Hello, world!");
              }

              /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com

              1 Reply Last reply
              0
              • N ns

                I have a function which is supposed to fill in abc: MyFunc(char* abc) { char* val = "aaa"; strcpy(abc,val); } Why do I have to strcpy? If I just do: { char* val = "aaa"; abc = val; } then there is garbage in abc when it returns. WHat exactly is happening if you do it my second way? Why doesnt it work?

                P Offline
                P Offline
                Philippe Mori
                wrote on last edited by
                #7

                Generally, it is better to uses std::string class instead of using raw pointers. Far more easier to uses and less error-prone. And for the rest, many people have already answer the question. Philippe Mori

                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