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. Passing by reference vs. Value

Passing by reference vs. Value

Scheduled Pinned Locked Moved C / C++ / MFC
questionvisual-studio
8 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.
  • L Offline
    L Offline
    LCI
    wrote on last edited by
    #1

    Is this correct? What i am attempting to ensure is that the arrErrCode[i]var is being passed by reference to the TestMyStuff method. The question is by using a LPTSTR vs. an &, i should get the same result correct? Main { For (int 1= 0; i<2; i++) { arrErrCode[i] = new TCHAR[16]; ZeroMemory(arrErrCode[i], 16 * sizeof(TCHAR)) if (TestmyStuff(arrErrCode[i])) //do something here with arrErrCode[i] } } BOOL TestMystuff(LPTSTR szCode) { StrCpy(szCode, TEXT("000")); return TRUE; }

    L C B 3 Replies Last reply
    0
    • L LCI

      Is this correct? What i am attempting to ensure is that the arrErrCode[i]var is being passed by reference to the TestMyStuff method. The question is by using a LPTSTR vs. an &, i should get the same result correct? Main { For (int 1= 0; i<2; i++) { arrErrCode[i] = new TCHAR[16]; ZeroMemory(arrErrCode[i], 16 * sizeof(TCHAR)) if (TestmyStuff(arrErrCode[i])) //do something here with arrErrCode[i] } } BOOL TestMystuff(LPTSTR szCode) { StrCpy(szCode, TEXT("000")); return TRUE; }

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      LCI wrote:

      BOOL TestMystuff(LPTSTR szCode)

      That is passing a pointer by value.

      led mike

      L 1 Reply Last reply
      0
      • L LCI

        Is this correct? What i am attempting to ensure is that the arrErrCode[i]var is being passed by reference to the TestMyStuff method. The question is by using a LPTSTR vs. an &, i should get the same result correct? Main { For (int 1= 0; i<2; i++) { arrErrCode[i] = new TCHAR[16]; ZeroMemory(arrErrCode[i], 16 * sizeof(TCHAR)) if (TestmyStuff(arrErrCode[i])) //do something here with arrErrCode[i] } } BOOL TestMystuff(LPTSTR szCode) { StrCpy(szCode, TEXT("000")); return TRUE; }

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Of course arrErrCode[i] var is passed by value, anyway, since it is a pointer then things would go on smootlhy (provided you fix all the bugs: your code is a bunch of typos). :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        1 Reply Last reply
        0
        • L led mike

          LCI wrote:

          BOOL TestMystuff(LPTSTR szCode)

          That is passing a pointer by value.

          led mike

          L Offline
          L Offline
          LCI
          wrote on last edited by
          #4

          So if i wanted to do it by reference then i would add an & to the function definition like : TestMyStuff(LPTSTR& szCode) { } I have never worked with LPTSTR's so i am just a tad confued with how to pass by reference using LPTSTR

          C C L 3 Replies Last reply
          0
          • L LCI

            So if i wanted to do it by reference then i would add an & to the function definition like : TestMyStuff(LPTSTR& szCode) { } I have never worked with LPTSTR's so i am just a tad confued with how to pass by reference using LPTSTR

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

            You don't need it. LPTSTR is just a reassuring name given to a pointer type. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            1 Reply Last reply
            0
            • L LCI

              So if i wanted to do it by reference then i would add an & to the function definition like : TestMyStuff(LPTSTR& szCode) { } I have never worked with LPTSTR's so i am just a tad confued with how to pass by reference using LPTSTR

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

              A LPTSTR is a pointer to a char array. So you don't need to pass it by reference unless you want to change it size (in fact, allocating a new array). If you only wants to modify the contents, then you can pass the pointer.

              Cédric Moonen Software developer
              Charting control [v1.3]

              1 Reply Last reply
              0
              • L LCI

                So if i wanted to do it by reference then i would add an & to the function definition like : TestMyStuff(LPTSTR& szCode) { } I have never worked with LPTSTR's so i am just a tad confued with how to pass by reference using LPTSTR

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                LCI wrote:

                So if i wanted to do it by reference then i would add an &

                Yes but the only reason to pass a pointer by reference is to change the value of the pointer (the address). Is that what you need to do? I would not guess that from the code you posted.

                led mike

                1 Reply Last reply
                0
                • L LCI

                  Is this correct? What i am attempting to ensure is that the arrErrCode[i]var is being passed by reference to the TestMyStuff method. The question is by using a LPTSTR vs. an &, i should get the same result correct? Main { For (int 1= 0; i<2; i++) { arrErrCode[i] = new TCHAR[16]; ZeroMemory(arrErrCode[i], 16 * sizeof(TCHAR)) if (TestmyStuff(arrErrCode[i])) //do something here with arrErrCode[i] } } BOOL TestMystuff(LPTSTR szCode) { StrCpy(szCode, TEXT("000")); return TRUE; }

                  B Offline
                  B Offline
                  Bram van Kampen
                  wrote on last edited by
                  #8

                  Hi There is much confusion, in particular amongst novices, about Pointers, References snd Values when used as arguments. Using a Value is clear, a copy of the value gets placed on the stack. You can do what you want with it, it will be lost when the stack is cut down when you return from the procedure. A pointer is also clear, You're passing on the address of some data. That data resides elsewhere, but you're given the access to the original. so Changing the data through the pointer affects the original. The problem here is that the Pointer may not point at valid memory. This is a condition that can only be checked at runtime. There is no way the compiler can use to check the validity of a pointer at Compile Time. When you use a Reference, you will get passed the address of the variable to your procedure, and the generated code is identical to passing a pointer. The difference is, that unlike passing pointers, the language syntax and semantics force you to enter the correct variable name. Hope this helps. :)

                  Bram van Kampen

                  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