• dejected_warp_core@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      edit-2
      5 hours ago

      *x = dereference or “point to”. Treats the variable x as containing a pointer value. Evaluates to a variable existing at the address in x.

      &x = reference or “get address of”. Evaluates to the address of x.

      They’re complimentary operators, so *(&x) cancels out and is equvalent to just x.

    • ZILtoid1991@lemmy.worldOP
      link
      fedilink
      arrow-up
      7
      arrow-down
      1
      ·
      15 hours ago

      The & operator references the value.

      int i;
      int* p = &i;
      

      In C++, the & at the function argument makes it a reference type (safe pointer).

      void someFunction(int& refVal) {
          [...]
      }
      
      • Flames5123@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        2
        arrow-down
        2
        ·
        14 hours ago

        As someone who likes working with higher level languages, I never understood the pass by reference or even referencing different pointers. It never stuck out to me as useful in what I want software to do. It’s too close to hardware.

        • Sylvartas@lemmy.dbzer0.com
          link
          fedilink
          English
          arrow-up
          10
          ·
          edit-2
          11 hours ago

          Most of the time you pass by reference for more outputs, or by const ref to avoid copying a big-ass data structure (which is not always straightforward, with structures smaller than a pointer, which are pretty big in 64 bits architecture, you lose more to the ref overhead than to the copy IIRC)

          • DahGangalang@infosec.pub
            link
            fedilink
            arrow-up
            4
            ·
            11 hours ago

            Another reason I commonly see: to change the structure / “main pointer” to a data structure (esp during freeing and cleanup).

        • lime!@feddit.nu
          link
          fedilink
          arrow-up
          10
          ·
          12 hours ago

          if you’re working with higher level languages you pass-by-reference all the time. give a list to a function to modify it? that’s by reference. giving an event handler function to a framework? that’s by reference. setting a property on an object? that’s usually by reference.

          • funkless_eck@sh.itjust.works
            link
            fedilink
            arrow-up
            1
            arrow-down
            1
            ·
            12 hours ago

            the list is the helpful part to understanding it.

            it would be terrible if, with bar being a list and foo being a member of the list

             if foo in bar: return true
            

            modified the list. So yeah, you want to look at the list not edit the list, it’s a pointer.

            • gerryflap@feddit.nl
              link
              fedilink
              arrow-up
              2
              ·
              10 hours ago

              Your example doesn’t make sense to me. Why would you modify the entire list by checking if something is in it? Also, you can totally edit the list via a pointer, that’s how you’re supposed to edit the list if you want any performance. Otherwise you’d be copying the list on every modification, which is terribly inefficient

            • lime!@feddit.nu
              link
              fedilink
              arrow-up
              4
              ·
              12 hours ago

              other way round surely? if you want to modify the original object, use a pointer. if you don’t, use a copy.

            • Sylvartas@lemmy.dbzer0.com
              link
              fedilink
              English
              arrow-up
              1
              ·
              edit-2
              9 hours ago

              A pointer to the list would allow you to modify whatever’s at that pointer’s address though. If you want to look at the list and not edit it you pass by const reference 99% of the time (or you pass a copy) and if your language doesn’t have that I don’t like it.

              • captcha_incorrect@lemmy.world
                link
                fedilink
                English
                arrow-up
                2
                ·
                8 hours ago

                I still don’t get the point of pointers.

                I want my language to pass by reference. I give a variable to a function and the variable in the function scope should be a reference to the same place in memory as the original variable.

                How can pointers help me here? What value does it provide? Genuine question.

                • Sylvartas@lemmy.dbzer0.com
                  link
                  fedilink
                  English
                  arrow-up
                  2
                  ·
                  edit-2
                  7 hours ago

                  I want my language to pass by reference. I give a variable to a function and the variable in the function scope should be a reference to the same place in memory as the original variable.

                  I’m not even a C++ wizard or anything (though it’s my most advanced language) so I’m not gonna argue that is good or bad, that sounds fine to me for a wide range of applications already.

                  But the way is see it, pointers kinda allow you to use “raw memory” which is an actual thing that’s gonna be handled by the program one way or another, and it’s a way to relatively refer to memory for example. As some guy on stackoverflow put it “That guy at the end for the bar” vs “Bob” can be very useful. Especially when using data structures you don’t know the size of at compile time.

        • ZILtoid1991@lemmy.worldOP
          link
          fedilink
          arrow-up
          1
          ·
          13 hours ago

          Reference values are quite useful, such as:

          double valOut;
          if (parseDouble(valOut) == 0) { //Argument of parseDouble is ref type, no & needed for input, no exceptions needed for error handling
              [...] //No error, code executed normally
          } else {
              [...] //Erroreus input
          }