Function prototypes:
1. int test (int & i);
2. int test (int * i);
Function calls:
1. test(n);
2. test(&n);
Can anyone explain the difference in as many aspects as possible? Thank you so much!
Function prototypes:
1. int test (int & i);
2. int test (int * i);
Function calls:
1. test(n);
2. test(&n);
Can anyone explain the difference in as many aspects as possible? Thank you so much!
References cannot be NULL
while pointers can be. That is basically the only difference. Generally, it's an implementation detail, so theoretically, pass by reference and pass by pointer could be different 'under the hood', however, they are almost always implemented in the same way.
The main difference is that a reference cannot possibly be null (in a well formed program), so there is no need to test for null. Other than that, references are usually implemented internally in terms of pointers, so the behavior will be similar.
Other than that, there is the style part of the problem. And in this case different conding styles will prefer one or the other. I have been in companies that preferred either style, the first because it is idiomatic in C++, the second because it seems to make it more explicit (at least for some programmers) that the function will be changing the argument if the argument is passed by pointer. My personal feeling is that this is more of a convention from C programmers, but nonetheless it exists.