• Iridium@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    1 year ago

    that’s the whole idea of passing by address in C

    Wait stop, so in other languages like C#, when you pass a variable into a function “by reference” is that just passing the pointer to the variable?

    Have I been baited into using pointers my whole life?

    • olorin99@kbin.social
      link
      fedilink
      arrow-up
      7
      ·
      1 year ago

      Yes passing “by reference” is essentially the same as “by pointer” but with some syntactical sugar to make it easier to work with.

    • pazukaza@lemmy.ml
      link
      fedilink
      arrow-up
      7
      arrow-down
      1
      ·
      edit-2
      1 year ago

      In C# it is different.

      In C if I give you a pointer to a memory address, you can totally overwrite what is in that memory address, even write a new struct in there. So you’re getting a “real” writable memory address.

      In languages like Java or C# you aren’t given a reference to the memory address but a reference to the object. You can only write to the object using it’s own interface (methods) but you can’t say “I’m going to totally overwrite this memory address with a new object”.

      If you receive an object in a parameter, let’s say a “Person person” object and you do something like “person = new Person();” you didn’t really overwrite the memory address. The original person reference that was passed in the parameter is still intact. You can only modify it with something like “person.setName(…)”.

      So, with real pointers you can do more stuff, but higher level languages don’t want you to do that because it breaks some of their principles for what “good programming” is.