views:

225

answers:

5

Please read before answering. I dont want you to state the obvious for me. THANKS :D

I am trying to differentiate between pointers and passing by value. And I think I understand them but one thing the source I was reading wasnt clear on is what is the difference between passing a pointer and passing by value. consider the following...

andy = 25;
fred = andy;
ted = &andy; 

The first line passes the value 25 to andy. The second line passes the value of andy to fred. Now fred possess the value 25. the third line passes reference to ted. Now ted holds the memory address of andy, which holds the value of 25. Now take a look at the next code

andy = 25;
fred = andy;
ted = &andy; 
joe = *andy;

The above code looks just like the first snippet except we pass the pointer of andy to joe. Now joe holds the pointer value of 25. Which almost seems the same as just doing (joe = andy). without *

what's the difference in passing the pointer value compared to just passing by value ???

In my interpretation, It appears that the pointer value of joe is still affected if later down the line I decided to change the value of andy/ compared to if I just passed by value, it not be affect. T

To my knowledge, the only difference between passing by reference and passing by pointer is that one holds the address, and the other holds the value within that address. which can still change if any varible that holds that value reference, decides to change it. If this is the case, what is the significance of passing by reference and passing by value.

Here is the link to what I was reading

http://www.cplusplus.com/doc/tutorial/pointers/

Thanks guys!!!

+5  A: 

What you're talking about has nothing to do with pass-by-reference or pass-by-value. Those terms only refer to what happens with parameters when function calls are made. The canonical example is the swap() function. This function:

void swap(int a, int b)
{
    int t = a;
    a = b;
    b = tmp;
}

doesn't do anything; the parameters won't be swapped if you call it, since they're passed by value. If you make them pass-by-reference (in C++) or you pass pointers (in C or C++), it will do what you want it to:

void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

or in C++ only,

void swap(int &a, int &b)
{
    int t = a;
    a = b;
    b = a;
}

The advantage of the pass-by-reference version is that it's guaranteed to get good references. The first example, if you pass bad pointers, will crash your program.

Carl Norum
Again, I am just reading and trying to learn. I am just regurgitating what this man is trying to explain http://www.cplusplus.com/doc/tutorial/pointers/
numerical25
+3  A: 

You can't do joe = *andy because andy isn't a pointer, it's an int.

However, you could do joe = *ted, because ted is a pointer.

Suppose you did this:

andy = 25; 
fred = andy; 
ted = &andy;  
joe = *ted;
andy = 26;
jim = *ted;

Then joe will have a value of 25, while jim will have a value of 26.

The reason for this is that ted is a pointer to andy. Using *ted gives you the value at that pointer, at that time.

Saxon Druce
I think I get it now. so anytime you pass a pointer on by value, You are not necessarily passing by value, but by reference. The only way to pass a reference on by value. is to use the *. ??
numerical25
Saxon Druce
lol, Ok, that explains why everyone is giving me these strange responses. Thanks!
numerical25
+1  A: 
Kevin
Yes, I understand now. And I appreciate the response, I definitely have a better grasp at it.
numerical25
+1  A: 

Your example shows example of assignment and referencing/dereferencing pointer/object, not passing by value or reference. And like what Saxon said above, you cant assign joe=*andy.

In simple you can define them as:

  • passing by value: you pass a copy of the value to a function. The copy existed within the scope of the function only. Any changes to it is local to the scope eg. void funcA(int a)
  • passing by reference: you pass an address of the object that hold the value to the function and the object still exist within its parent scope. You have the address of the object that is in your parent scope so changes will be made to that same object. eg. void funA(int& a)
  • passing by pointer: you pass a pointer that hold the address to that object. You are actually passing the pointer as a value (hold the address to the object) into the method. If you change the pointer to point to another object, the original object in the parent scope doesnt change. If you want to change the object it points to, then you have to deference it. eg. void funA(int* a)
Fadrian Sudaman
A: 
darasan