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!!!
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: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:
or in C++ only,
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.
What is the significance of passing by reference and passing by value?
You're right, in this context there is no difference between
(a) assigning the values directly, eg (
joe = andy
) and(b) Copying the memory address and then de-referencing eg (
ted = &andy; joe = *ted;
)There is no difference because they have the same scope, i.e. they're in the same function. The significance of passing pointers only kicks in once you start passing them to other functions.
When I was trying to understand pointers (just 6 months ago), the main eureka moment for me was (and no-one ever tells you this)...in C, functions pass their parameters by copying them. This is significant because it means that if you just pass variables to other functions, changes to those variables won't be reflected in the other functions in your program.
Lets say you have a program that tracks the number of students in a class. When a new student joins the class, a function AddStudent is called to increment the number of students in the class. There are 2 versions of AddStudent here, pass by reference and pass by value. We'll call each from main, and printf the results to see the difference.
Hope this helps...
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:
I think you're just about there, but your confusing the terminology. Let look at your examples again:
The first line assigns the value 25 to andy. The second line assigns the value held by andy to fred (25 in this case). The third line assigns the address of andy to ted.
All three variables hold independent values. Changing any one of them will not affect the others.
Now, looking at your next example:
Like you said, the first three lines are the same. The fourth line assigns the value pointed to by andy to joe. In this case joe would now hold the value at memory location 25 because andy is being treated as a memory location (and the actual value is unknown in this case because we don't know from the data given what is stored at address 25).
And, again, all four variable hold independent values at this point. Changing any of them will not affect the value of the others.
So lets look at another example:
The first line assigns the value of 25 to andy. Nothing new here. The second line assigns the address of andy to jim. The third line dereferences the address stored in jim and assigns the value to alex. The variable alex now holds the value 25. But the 25 that alex holds is a different 25 than what alan holds because alan and alex live at different memory addresses. If we change the value of alan to 30, alex still hold 25.
A final example:
The first two lines are just like the previous example, andy has a value of 25 and jim contains the address of andy.
The third line assigns a value of 30 to the memory address pointed to by jim. Since this is the same address that andy lives at, andy now hold a value of 30. In this case andy and *jim reference the same memory location.
The terms pass-by-value and pass-by-pointer (or pass-by-reference) refer to passing parameters to functions. When passing-by-value, as in:
void myfunc(int a) { a = a+5; }
And then called as in:
The value of b, upon return from the function is not changed because you merely passed the value of b into the function, assigning it to a. The variable a was changed but since it lives at a different memory address, changing a did not affect the value of b.
But when passing-by-pointer, as in:
void myfunc(int* a) { *a = *a+5; }
And then called as in:
The value of b is changed from 2 to 7 because you passed the address of b to the function and then dereferenced the address to getit's current value, added 5 to it, and stored the result back in to the same address. When the function returns, the value stored at the address of b has been updated.
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:
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.