# Thread: different between passing a pointer

1. ## different between passing a pointer

lets take method test which returns int

int test(int * p);
int test(int& p);

please can someone explain the different between above two stateme

2. What do you think are the differences between the two?

3. Originally Posted by laserlight
What do you think are the differences between the two?
there is a different but i cannot understand.please someone help me with the explanation

4. Well, I'm asking you to give answering your own question a shot. Think: how would you call these two functions? If you are implementing them, how would you use the parameter if you want to access what the parameter refers to? What happens if the argument for the former is a null pointer?

5. below is my testing code.in here test will take a pointer as argument.but what is the different between test1 and test 2.both take integers as argument.i cant understand different between them.thank you for help me to think it out.can you explain test1 and 2?

Code:
#include<iostream>

using namespace std;

int test(int * p)
{
return (*p)+1;
}

int test1(int& p)
{
return p+1;
}

int test2(int p)
{
return p+1;
}

int main()
{
int a=1;
cout<<test(&a)<<endl;
cout<<test1(a)<<endl;
cout<<test2(a)<<endl;

}

6. Originally Posted by rukshan
what is the different between test1 and test 2.both take integers as argument.i cant understand different between them.thank you for help me to think it out.can you explain test1 and 2?
Compile and run:
Code:
#include <iostream>

using namespace std;

void test(int* p)
{
*p = *p + 1;
}

void test1(int& p)
{
p = p + 1;
}

void test2(int p)
{
p = p + 1;
}

int main()
{
int a = 1;
test(&a);
cout << a << endl;

a = 1;
test1(a);
cout << a << endl;

a = 1;
test2(a);
cout << a << endl;
}
What do you observe, and what does it tell you?

7. output :test - 2
test1 - 2
test2 -2

Answer one is according to pointer Arithmetic .I can understand that.
Answer 3 is according to normal integer passing.What i think is test1 and test2 are equal am i right?

8. Are you sure you compiled the code that I showed you? The output that I expect is:
Code:
2
2
1

9. Originally Posted by rukshan
output :test - 2
test1 - 2
test2 -2

Answer one is according to pointer Arithmetic .I can understand that.
Answer 3 is according to normal integer passing.What i think is test1 and test2 are equal am i right?
About test: It's not necessarily pointer arithmetic if a dereference is in the expression. It means that the pointee is accessed and it could just be regular arithmetic, as is the situation here. That might be pedantic of me, but pointer arithmetic is totally different, and you could make a critical mistake.

test and test1 do the same thing, but you should notice the difference in syntax and how aliasing is different from pass by value semantics. For example, your guess at the output of the program is wrong.

10. Originally Posted by whiteflags
About test: It's not necessarily pointer arithmetic if a dereference is in the expression. It means that the pointee is accessed and it could just be regular arithmetic, as is the situation here. That might be pedantic of me, but pointer arithmetic is totally different, and you could make a critical mistake.

test and test1 do the same thing, but you should notice the difference in syntax and how aliasing is different from pass by value semantics. For example, your guess at the output of the program is wrong.
still i cannot understand different between test1(int p) and test2(int& p).
what is the different between int p and int& p which are passing as arguments

11. Originally Posted by rukshan
still i cannot understand different between test1(int p) and test2(int& p).
what is the different between int p and int& p which are passing as arguments
test2(int& p); ... pass by reference. changes to p inside test2() will be reflected back to the calling function.

test1(int p); ... pass by value, a copy of the value is passed, any changes to p inside of test1() are local, the value in the calling function will not change
Kurt