difference between int* and int&

Can anyone please explain the difference between these two programs ?
Program1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std ;

void swap(int* , int*);

int main () {
 int a , b ;
 cout << "Enter two numbers ";
 cin >>a >> b ;
 cout << "\nOld values :  a = " << a << "\tb = " << b ;
 swap(&a , &b);
 cout << "\nNew values : a = " << a << "\tb = " << b ;
 return 0 ;
}

void swap(int *p1 , int *p2){
 int temp ;
 temp = *p1 ;
 *p1 = *p2 ;
 *p2 = temp ;
}



Second Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std ;

void swap(int& , int&);

int main () {
 int a , b;
 cout << "Enter two numbers ";
 cin >>a >> b ;
 cout << "\nOld values : a = " << a << "\tb = " << b ;
 swap(a,b);
 cout << "\nNew values : a = " << a << "\tb = " << b ;
 return 0 ;
}

void swap(int &x , int &y){
 int temp ;
 temp = x ;
 x = y ;
 y = temp ;
}
First one is yields Passes a *pointer by value.
The caller can change the pointer (the parameter), but that
won't change the temporary pointer created on the call side (the argument).
so in final result values will not swapped.

Whereas in second one is pass by reference so value will be swapped after call of function.. you better read concept of pass by value and pass by reference that will help you further..
HiteshVaghani1 wrote:
The caller can change the pointer (the parameter), but that
won't change the temporary pointer created on the call side (the argument).
so in final result values will not swapped.

It will not swap the pointers but it will swap the values that the pointers point to.
closed account (o1vk4iN6)
^ even i don't know what your saying...

They do the same thing, only different is that you don't need to use &* to get the pointer and to set the value of the pointer, it is done automatically.

The concept of reference's was introduced in C++. It is meant to hide pointers from the user but still have the same effect as pointers.
The difference is that in the second case, void swap(int &x , int &y) works directly with main()'s a and b. The name "x" describes the same object as the name "a", and the name "y" describes the same thing as the name "b". This is known as "pass-by-reference"

In the first case, the program is more complex: it first constructs two nameless pointers (one constructed by the expression &a, the other by the expression &b), then it copies them to the function swap(), so that "p1" is now a copy of the pointer produces by &a, and "p2" is a copy of the pointer produced by "&b". Then it uses dereferencing (*p1 and *p2) to access the objects that are referenced by those pointers.

The end result is the same, and the compilers will likely optimize out the pointers and produce identical machine code, but the second version is simpler and safer to use. Of course, in real life C++ already has the function swap() that works with references and iter_swap() that works with pointers, so both programs are redundant.
Last edited on
The difference is that you can crash one of them by passing it a null pointer...
closed account (o1vk4iN6)
1
2
3
4
5
6
7
8

void swap(int & a, int & b)
{
/* ... */
}

swap( *(int*)0, *(int*)0 );


So I guess that means passing by pointer doesn't crash with a null pointer :).
thanks a lot guys ..
what I understand now is that
"Both the cases are of call by reference.

In first case , the addresses are stored in pointers and these are dereferenced within the function."

In second case the actual object ie a and b is same as x and y .This means if I change the value of x to 10 , the value of a will be 10 as well !!!. "
Both the cases are of call by reference.


We're heading into the flame-war here about this, but to my mind, the first is still pass-by-value. You pass an object, and a copy of that object is made and given to the function. This is pass-by-value. In this case, that object happens to be a pointer, but that makes no difference - a copy is made, the copy is given to the function. Only the second is pass-by-reference.

But yes, your understanding of what's happening is correct.
well Moschops certainly made a point about the object pointers being copied as &x is same as &a . but is it pass-by-value ..I am not sure about this. More discussion might make things more clear.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std ;

void swap(int , int);

int main () {
 int a , b;
 cout << "Enter two numbers ";
 cin >>a >> b ;
 cout << "\nOld values : a = " << a << "\tb = " << b ;
 swap(a,b);
 cout << "\nNew values : a = " << a << "\tb = " << b ;
 return 0 ;
}

void swap(int x , int y){
 int temp ;
 temp = x ;
 x = y ;
 y = temp ;
}

This is an example of call by value.
Referring to the above program , the values remain same in the main program as the values are copied to x and y and all the changes are made onto x and y , NOT a and b.



Topic archived. No new replies allowed.