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. Pass by reference

Pass by reference

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelpdiscussion
6 Posts 4 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
    Jeff Patterson
    wrote on last edited by
    #1

    I recently was in a little discussion and a question came up. Does a variable that is passed by reference have the same address as it would if it was pointed to? How would I prove or diprove this? Thanks for the help in advance.:confused: Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]

    A J M 3 Replies Last reply
    0
    • J Jeff Patterson

      I recently was in a little discussion and a question came up. Does a variable that is passed by reference have the same address as it would if it was pointed to? How would I prove or diprove this? Thanks for the help in advance.:confused: Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]

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

      Yes they are. To prove it: BOOL TestAddr(int& ref, int* ptr) { return (DWORD)&ref == (DWORD)ptr; } void main() { int n; assert(TestAddr(n, &n)); }

      J 1 Reply Last reply
      0
      • J Jeff Patterson

        I recently was in a little discussion and a question came up. Does a variable that is passed by reference have the same address as it would if it was pointed to? How would I prove or diprove this? Thanks for the help in advance.:confused: Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]

        J Offline
        J Offline
        JWood
        wrote on last edited by
        #3

        void f(int& v) { printf("var in function: %u\n", &v); } int main() { int v; printf("var in main: %u\n", &v); f(v); } If these outputs are the same, then the pointers are the same. These HW assignments are fun. Even a monkey could do them. J. ----------------------------

        J 1 Reply Last reply
        0
        • J Jeff Patterson

          I recently was in a little discussion and a question came up. Does a variable that is passed by reference have the same address as it would if it was pointed to? How would I prove or diprove this? Thanks for the help in advance.:confused: Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]

          M Offline
          M Offline
          melwyn
          wrote on last edited by
          #4

          A reference is nothing but a constant pointer.

          1 Reply Last reply
          0
          • A Abin

            Yes they are. To prove it: BOOL TestAddr(int& ref, int* ptr) { return (DWORD)&ref == (DWORD)ptr; } void main() { int n; assert(TestAddr(n, &n)); }

            J Offline
            J Offline
            Jeff Patterson
            wrote on last edited by
            #5

            Thanks this and the other above did the trick. :) Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]

            1 Reply Last reply
            0
            • J JWood

              void f(int& v) { printf("var in function: %u\n", &v); } int main() { int v; printf("var in main: %u\n", &v); f(v); } If these outputs are the same, then the pointers are the same. These HW assignments are fun. Even a monkey could do them. J. ----------------------------

              J Offline
              J Offline
              Jeff Patterson
              wrote on last edited by
              #6

              Thanks, this and the other response were exactly what I was trying to prove. Unfortunately I'm not proficient enough to have done this myself. Thanks again.:) Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]

              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