In order to heighten retention, I decided to rewrite the example and provide comment equivalent to the discussion that would be had in a classroom environment.
First, the code with no comments:
#include <iostream>
using namespace std;
void change_value(int *my_ptr) {
*my_ptr += 10;
}
void change_value (int val) {
val = 10;
}
int main() {
int value = 5;
int *value_ptr;
value_ptr = &value;
cout << "value: " << value << endl;
cout << "*value_ptr: " << *value_ptr << endl;
change_value(value);
cout << "change_value(value);" << endl;
cout << "value: " << value << endl;
change_value(*value_ptr);
cout << "change_value(*value_ptr);" << endl;
cout << "value: " << value << endl;
change_value(value_ptr);
cout << "change_value(value_ptr);" << endl;
cout << "value: " << value << endl;
change_value(&value);
cout << "change_value(&value);" << endl;
cout << "value: " << value << endl;
cin >> value;
return 0;
}
Can you guess the values of value each time it is printed out?
Write down or remember your answer, then compile and run.
Don't worry if you made a mistake, that's why you're here!
The correct order of values is:
Spoiler
Lets walk through it a couple lines at a time.
Prep Work:
//We need std::cout from <iostream> to print to the screen. #include <iostream> //And I hate typing std:: multiple times so we use the std namespace. using namespace std;
"Working" change_value function:
//Our "working" change_value function. Given an int pointer,
//it will increase the value of the int that the pointer is pointing to by ten.
void change_value(int *my_ptr) {
*my_ptr += 10;
}
"Dud" change_value function:
//Our "dud" change_value function. It takes in an int by value, thus the variable
//itself is never passed in but instead its value, equivelent to int(val) where
//val is the variable supplied.
void change_value (int val) {
val = 10;
}
main() and variable declaration:
int main() {
//declare an int variable named "value" and assign it the value 5
int value = 5;
//declare a pointer to an int;
int *value_ptr;
//give that pointer the address of the int variable "value"
value_ptr = &value;
//print out the values of "value" and the int to which "value_ptr" points.
cout << "value: " << value << endl;
cout << "*value_ptr: " << *value_ptr << endl;
Our first attempt at increasing the value of "value":
//Does not change value of "value" because we are passing by value
//and the variable "value" will never be touched.
change_value(value);
cout << "change_value(value);" << endl;
cout << "value: " << value << endl;
That didn't work, shame. Let's try again:
//Does not increase "value" by ten because we are dereferencing before passing;
//thus we are passing an int, not a int pointer. See previous comment.
change_value(*value_ptr);
cout << "change_value(*value_ptr);" << endl;
cout << "value: " << value << endl;
Darn, still got not doing anything there either. I know, lets try this:
//Ahh, finally a change_value call that actually changes the value of "value".
//Notice that we are NOT dereferencing the point
change_value(value_ptr);
cout << "change_value(value_ptr);" << endl;
cout << "value: " << value << endl;
Success! But everybody knows there's more than one way to do the same thing. Does this work?
//Finally, it is worth noting that you do not need to create a pointer variable
//in use a function that takes a pointer. Just reference the variable you wish
//to pass in using '&'.
change_value(&value);
cout << "change_value(&value);" << endl;
cout << "value: " << value << endl;
return 0;
It does! Well, now that we've covered all the possibilities, you should be a little more prepared for experimenting with pointer use in your functions. Don't get overwhelmed. Slow down and think about what you are trying to accomplish and the syntax will come to you. When you think "address of" type '&' and when you think "value of the variable the pointer is pointing to" type '*'.
Good luck D.I.C.'lets
The full, commented source code.
#include <iostream>
using namespace std;
//Our "working" change_value function. Given an int pointer,
//it will increase the value of the int that the pointer is pointing to by ten.
void change_value(int *my_ptr) {
*my_ptr += 10;
}
//Our "dud" change_value function. It takes in an int by value, thus the variable
//itself is never passed in but instead its value, equivelent to int(val) where
//val is the variable supplied.
void change_value (int val) {
val = 10;
}
int main() {
//declare an int variable named "value" and assign it the value 5
int value = 5;
//declare a pointer to an int;
int *value_ptr;
//give that pointer the address of the int variable "value"
value_ptr = &value;
//print out the values of "value" and the int to which "value_ptr" points.
cout << "value: " << value << endl;
cout << "*value_ptr: " << *value_ptr << endl;
//Does not change value of "value" because we are passing by value
//and the variable "value" will never be touched.
change_value(value);
cout << "change_value(value);" << endl;
cout << "value: " << value << endl;
//Does NOT increase "value" by ten because we are dereferencing before passing;
//thus we are passing an int, not a int pointer. See previous comment.
change_value(*value_ptr);
cout << "change_value(*value_ptr);" << endl;
cout << "value: " << value << endl;
//Ahh, finally a change_value call that actually changes the value of "value".
//Notice that we are NOT dereferencing the point
change_value(value_ptr);
cout << "change_value(value_ptr);" << endl;
cout << "value: " << value << endl;
//Finally, it is worth noting that you do not need to create a pointer variable
//in use a function that takes a pointer. Just reference the variable you wish
//to pass in using '&'.
change_value(&value);
cout << "change_value(&value);" << endl;
cout << "value: " << value << endl;
cin >> value; //in place for those not executing from the command line, prevents window from disappearing.
return 0; // return success to the operating system
}
This post has been edited by JackOfAllTrades: 16 October 2011 - 05:25 AM
Reason for edit:: Changed void main() to int main()






MultiQuote



|