-1

As you can see in the example I'm about to provide below I'm using both Pass By Reference and Pass By Address...

#include<iostream>
#include<string>

void passByAddress(int *a);
void passByReference(int &a);

int main() {
    int betty = 21;
    int setty = 45;

    passByAddress(&betty);
    passByReference(setty);

    std::cout << "Betty : " << betty << std::endl;
    std::cout << "Setty : " << setty << std::endl;
}

//Pass By Adress
void passByAdrress(int *a) {
    *a = *a + 5;
    //Memory Adress of a.
    //So gives hexa decimal.
    std::cout << "Address : " << a << std::endl;
    //Actual Value of adress
    std::cout << "Address's Value : " << *a << std::endl;
}
//Pass By Reference
void passByReference(int &a) {
    a = a + 5;
    //Memory Address of a.
    //So gives hexa decimal.
    std::cout << "Adrress : " << &a << std::endl;
    //Actual Value of address
    std::cout << "Address's Value " << a << std::endl;
}

So here I really don't understand the difference in using passbyaddress and passbyreference..Although i do understand the differences between these both and Pass By Value(As pass by value passes the copy of variable not memory address), I don't understand whats the difference of these both. Many people just say "Use Pass By Reference its much better", or "Use Pass By Reference all the time except when you have to use Pass By Address".. But I want to know the real difference so i can decide which one to use in my later projects.

Thankyou.

dinotom
  • 4,990
  • 16
  • 71
  • 139
amanuel2
  • 4,508
  • 4
  • 36
  • 67
  • 1
    The difference to me is all of those `*`s that you do not have in the second example. – NathanOliver Apr 26 '16 at 12:15
  • Thats it? @NathanOliver ? – amanuel2 Apr 26 '16 at 12:16
  • I would suggest looking at the assembly code that your above example generates. I usually just look at the disassembly window in Visual Studio. On my system the above code translates to the same thing. – Mohamad Elghawi Apr 26 '16 at 12:18
  • The difference, at least in this case, is mostly semantic. It's not unlikely that the compiler treats references as pointer "under the hood", and might even generate almost the exact same code for both functions and both calls. – Some programmer dude Apr 26 '16 at 12:18
  • the main difference is that pointers can be `0` while references always reference something. Checking for null pointers can be tedious and is most often not necessary. However sometimes a null pointer must be a valid parameter, in that case you "have to use Pass By Adress" as mentioned by "many people" – 463035818_is_not_an_ai Apr 26 '16 at 12:19
  • 1
    Exactly @AlessandroTeruzzi ! It says: My rule of thumb is: Use pointers if you want to do pointer arithmetic with them (e.g. incrementing the pointer address to step through an array) or if you ever have to pass a NULL-pointer. Use references otherwise. .. Well first of all what does it mean when it says If you ever have to pass a NULL-Pointer? – amanuel2 Apr 26 '16 at 12:20
  • Wow that gave me a huge clue @tobi303 !!! But one more question, can you give me an example of how to make a pointer a NULL Pointer. – amanuel2 Apr 26 '16 at 12:34
  • 1
    making a pointer a null pointer: `int* p = 0` – 463035818_is_not_an_ai Apr 26 '16 at 12:35
  • Thanks a lot @tobi303 ! I think i should just use Pass By Refrence since its more readble, and i dont have to pass the conflict of NULL Pointers.. Thanks a lot! – amanuel2 Apr 26 '16 at 12:39

1 Answers1

-1

Internally both usually do the same: pass pointer. But when you pass the reference you cannot directly do things like *(p + 1)

There are much more explations here https://www.quora.com/What-is-the-difference-between-pass-by-reference-and-pass-by-pointers-to-a-function

GMichael
  • 2,726
  • 1
  • 20
  • 30
  • Hmm Michael... What do you mean directly do things like *(p + 1) ?? – amanuel2 Apr 26 '16 at 12:22
  • @dsa if you don't know what is `*(p + 1)` then you need to read more about pointers. Basically you can apply some operations on the pointer it self to "jump" to other elements without losing the pointer. `*(p + 1)` would mean "_go to the neighbor element of this memory address(p) and get me its value_" – Khalil Khalaf Apr 26 '16 at 12:31
  • I watched many tutorials about it but i didnt get *(p+1) ????????? – amanuel2 Apr 26 '16 at 12:32
  • @dsa if you still can't get what I wrote.. Well.. You need to watch/read more.. – Khalil Khalaf Apr 26 '16 at 12:33