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. How do you pass an int by reference to a function?

How do you pass an int by reference to a function?

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 4 Posters 21 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.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    int AnInt=6; void EditInt(int * IntPnt) { *IntPnt=7; } EditInt(&AnInt); Is this how you should proceed?

    Greg UtasG 1 Reply Last reply
    0
    • C Calin Negru

      int AnInt=6; void EditInt(int * IntPnt) { *IntPnt=7; } EditInt(&AnInt); Is this how you should proceed?

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      That's passing as a pointer, not as a reference. As a reference (C++ only, though maybe C now has them too), it's

      void EditInt(int& IntRef)
      {
      IntRef=7;
      }

      int AnInt=6;
      EditInt(AnInt);

      Robust Services Core | Software Techniques for Lemmings | Articles
      The fox knows many things, but the hedgehog knows one big thing.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      L C 2 Replies Last reply
      0
      • Greg UtasG Greg Utas

        That's passing as a pointer, not as a reference. As a reference (C++ only, though maybe C now has them too), it's

        void EditInt(int& IntRef)
        {
        IntRef=7;
        }

        int AnInt=6;
        EditInt(AnInt);

        Robust Services Core | Software Techniques for Lemmings | Articles
        The fox knows many things, but the hedgehog knows one big thing.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Greg Utas wrote:

        That's passing as a pointer, not as a reference.

        Which is just the same, apart from semantics.

        Greg UtasG 1 Reply Last reply
        0
        • Greg UtasG Greg Utas

          That's passing as a pointer, not as a reference. As a reference (C++ only, though maybe C now has them too), it's

          void EditInt(int& IntRef)
          {
          IntRef=7;
          }

          int AnInt=6;
          EditInt(AnInt);

          Robust Services Core | Software Techniques for Lemmings | Articles
          The fox knows many things, but the hedgehog knows one big thing.

          C Offline
          C Offline
          Calin Negru
          wrote on last edited by
          #4

          I got it, thank you.

          1 Reply Last reply
          0
          • L Lost User

            Greg Utas wrote:

            That's passing as a pointer, not as a reference.

            Which is just the same, apart from semantics.

            Greg UtasG Offline
            Greg UtasG Offline
            Greg Utas
            wrote on last edited by
            #5

            Very true. A reference can even be to nullptr, and you need to check for this if writing bulletproof code:

            void function(type& arg)
            {
            if(&arg == nullptr)
            throw std::invalid_argument("function received null argument");
            }

            type* nasty = nullptr;
            function(*nasty);

            Without the check, function crashes when it gets around to using arg.

            Robust Services Core | Software Techniques for Lemmings | Articles
            The fox knows many things, but the hedgehog knows one big thing.

            <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
            <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

            L honey the codewitchH 2 Replies Last reply
            0
            • Greg UtasG Greg Utas

              Very true. A reference can even be to nullptr, and you need to check for this if writing bulletproof code:

              void function(type& arg)
              {
              if(&arg == nullptr)
              throw std::invalid_argument("function received null argument");
              }

              type* nasty = nullptr;
              function(*nasty);

              Without the check, function crashes when it gets around to using arg.

              Robust Services Core | Software Techniques for Lemmings | Articles
              The fox knows many things, but the hedgehog knows one big thing.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              And the same with a pointer, since there is no material difference between the two.

              1 Reply Last reply
              0
              • Greg UtasG Greg Utas

                Very true. A reference can even be to nullptr, and you need to check for this if writing bulletproof code:

                void function(type& arg)
                {
                if(&arg == nullptr)
                throw std::invalid_argument("function received null argument");
                }

                type* nasty = nullptr;
                function(*nasty);

                Without the check, function crashes when it gets around to using arg.

                Robust Services Core | Software Techniques for Lemmings | Articles
                The fox knows many things, but the hedgehog knows one big thing.

                honey the codewitchH Offline
                honey the codewitchH Offline
                honey the codewitch
                wrote on last edited by
                #7

                I would assert() something like that rather than throw. It strikes me as an error in the code itself, rather than something that should have filtered its way into runtime code, so I think assert is more appropriate. As I understand it, best practices indicate that a reference should never be deliberately null. Could be wrong here?

                Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                Greg UtasG 1 Reply Last reply
                0
                • honey the codewitchH honey the codewitch

                  I would assert() something like that rather than throw. It strikes me as an error in the code itself, rather than something that should have filtered its way into runtime code, so I think assert is more appropriate. As I understand it, best practices indicate that a reference should never be deliberately null. Could be wrong here?

                  Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                  Greg UtasG Offline
                  Greg UtasG Offline
                  Greg Utas
                  wrote on last edited by
                  #8

                  What to do depends on the environment you're running in. I've never used assert, so I had to look up the documentation, which says that it calls abort. Most application code won't catch an invalid_argument exception, so the effect will be similar. In my open-source software, I neither throw nor assert in this situation. Instead, I generate a debug log with a traceback and return whatever signifies failure if the function has a result. That's appropriate if avoiding crashes is paramount, which is what my software aims for. You're right that a reference shouldn't be null. For many functions, a pointer argument shouldn't be null either. But applications misbehave, so you have to decide whether your function will check for bad arguments or just crash.

                  Robust Services Core | Software Techniques for Lemmings | Articles
                  The fox knows many things, but the hedgehog knows one big thing.

                  <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                  <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                  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