0
void funct( int *val1, int &val2)
{
    *val1= 20;
    val2= 22;
}

int main()
{
    int v1=10, v2=12;
    funct(&v1, v2);
    cout<<"v1: "<<v1<<endl<<"v2: "<<v2<<endl;
    return 1;
}

These both, v1 and v2 are called by reference by funct(). But I don't understand, what is difference between both ways apart from syntactical difference. Few questions I want to ask are,

  • Are both totally same thing?
  • Are both equally good in terms of coding practice?
  • Situations where one will work, other don't?
  • What are other things that I can do using one, that I can't do using other?

To simplify, I would call:

int *val1 ------- Method 1
int &val2 ------- Method 2
impossible
  • 2,380
  • 8
  • 36
  • 60
  • Unless you can provide a link to the specification of that language C/C++, we have assume it does not exist. But there are the two **different** languages C and C++. Your code is apparently C++, so do not add C tag! – too honest for this site Jan 19 '16 at 15:42
  • And `v1`/`val1` is not **passed** by-reference, but by value. Note also there is no "**call** by reference" (here). – too honest for this site Jan 19 '16 at 15:43
  • Try to get the value of `val1` and `val2`! – too honest for this site Jan 19 '16 at 15:44
  • *These both, v1 and v2 are called by reference by funct()* -- `v1` is not called by reference. Passing an address is a by-value call. The `&` in the case of `v1` is **address-of**, not *reference*. Maybe that is where you're confused -- the same symbol, `&` is used for two different purposes, address-of and `reference`. – PaulMcKenzie Jan 19 '16 at 15:44
  • 1
    Possible duplicate of [What are the differences between a pointer variable and a reference variable in C++?](http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) – P.P Jan 19 '16 at 15:48

3 Answers3

2

I guess you are trying to understand the functioning of call by references and pointers. For more and better understanding I recommend you to read answers from this question.

Are there benefits of passing by pointer over passing by reference in C++?

Good Luck!

Community
  • 1
  • 1
1

Getting to your questions:

  • Are both totally same thing? Yes and no. You might get the same resulting machine code, but you use them in the function differently as you see in your own code. Because you cannot change those pointers and you really don't want to, a reference makes more sense.

  • Are both equally good in terms of coding practice? In C you have no option but in C++ you should use references. If you pass arrays you might think about though there exist C++ arrays (std::arrays) which can be referenced as well. You still can use pointers for return-by-reference method because C++ knows C mostly.

  • Situations where one will work, other don't? For C arrays a constant pointer might help, but you have data structures by STL (vector and array) which can do the same and with references.

  • What are other things that I can do using one, that I can't do using other? Well, pointers are more powerful than references but I think the additional power you get compared does not really help you if you want to pass values by reference. Also remember you can still return one value which also could be composed by struct or class. If you prefer to return values, only one is possible (unlike in Matlab for example), but you can return them as bundles through other datatype definitions.

NOTE: Read the provided link by @hardydragon with an awesome explanation

About References in C++:

Though the question is explained in numerous books about C++, let me try to focus in your specific question.

In C there only exists the pointer. You can pass them as being the "reference". When C++ was introduced references came into place where you can define

int &val2

But here is the catch, the reference usually does the same thing as the pointer, but you cannot change address, because there is no direct address-access to the variable. If you access a C++ reference it will give you the value you are pointing/referencing and it will allow you to modify that value; they work like normal variables. On the downside in its definition you have to tell the reference where it references immediately:

int a = 5; int &b = a; // If you pass by reference it has the same effect for the given parameter to your function

Pointers can point at runtime to null, to something invalid or to whatever you want within the memory. You can change the address at any part. Also you can do pointer arithmetic which is not possible with references. Of course that is dangerous, but you might already know why.

The problem about pointers is/was, that people get/got confused with them, especially the dereference (&) operator and pointer (*) definition is/was not clear for many. Considering pointer-to-pointers does not really improve the situation. So using references like it is in C++ made the live easier for many people. This does not mean that you should not understand pointers. The point is your code becomes more readable.

So as general hint because you use C++, use references. It is not always possible to use them due it's limitations, but wherever possible use them.

So this is my opinion but for call by reference you should use references, unless there are reasons you can't.

Gerhard Stein
  • 1,543
  • 13
  • 25
0

In terms of the functionality of the code that you've provided, there's no difference between these two functions:

void swap_with_addresses(int * a, int * b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

void swap_with_references(int & a, int & b) {
    int tmp = a;
    a = b;
    b = tmp;
}

int main() {
    int a = 5, b = 10;
    std::cout << a << "," << b << std::endl;
    swap_with_addresses(&a, &b);
    std::cout << a << "," << b << std::endl;
    swap_with_references(a, b);
    std::cout << a << "," << b << std::endl;
    return 0;
}

Executing this code yields:

5,10
10,5
5,10
Xirema
  • 19,889
  • 4
  • 32
  • 68
  • Yes. I know about that, but it doesn't help me answer all other questions that I have mentioned above. Could you please help me with those? – impossible Jan 19 '16 at 15:54
  • I was going to, but @hardydragon already provided an answer with a link that says everything I would have said myself. – Xirema Jan 19 '16 at 15:55