Posted: Mon Apr 16, 2007 10:04 pm
Yikes... When dealing with pointers, relying on things like "it just works" is a bad idea. You could be lucking out at the moment but when running it under slightly different conditions, you could be trampling some memory that you shouldn't be touching. When you're passing in a variable like this:
void function(int &a) { ... }
you're using a reference, not a pointer. It's basically letting the compiler figure out the pointer stuff for you. Doing "a = 2" will indeed set the value of the variable back in the location where you called the function. When you use an actual pointer like this:
void function(int *a) { ... }
doing the same "a = 2" is not going to change the value of a back where you called the function. a is a pointer to an int, so doing "a = 2" changes the location that the pointer is pointing to. If you're trying to change the value of the variable that you passed to the function you need to dereference the pointer by doing "*a = 2", just like Luben and thephoenix said. And of course back in the initial function call, you need to pass the variable like so:
function(&a);
In that case, & is the "address of" operator, not the reference operator. Getting the address of a variable gives you a pointer. Isn't C++ great? Personally, I never use references because of that stupid syntax. I cut my teeth on C where all you had was pointers and no references. And damnit, things made sense!
To sum it all up, here's an example:
int a = 0;
refFunc(a); // Good. a now equals 1.
ptrFunc1(&a); // Good. a now equals 2.
ptrFunc2(&a); // Doesn't work. a still equals 2.
ptrFunc1(a); // This should be a compiler error.
refFunc(int &arg) {
arg = 1;
}
ptrFunc1(int *arg) {
*arg = 2;
}
ptrFunc2(int *arg) {
arg = 3; // Setting the value of the pointer, not the intended variable.
}
Make sense?
void function(int &a) { ... }
you're using a reference, not a pointer. It's basically letting the compiler figure out the pointer stuff for you. Doing "a = 2" will indeed set the value of the variable back in the location where you called the function. When you use an actual pointer like this:
void function(int *a) { ... }
doing the same "a = 2" is not going to change the value of a back where you called the function. a is a pointer to an int, so doing "a = 2" changes the location that the pointer is pointing to. If you're trying to change the value of the variable that you passed to the function you need to dereference the pointer by doing "*a = 2", just like Luben and thephoenix said. And of course back in the initial function call, you need to pass the variable like so:
function(&a);
In that case, & is the "address of" operator, not the reference operator. Getting the address of a variable gives you a pointer. Isn't C++ great? Personally, I never use references because of that stupid syntax. I cut my teeth on C where all you had was pointers and no references. And damnit, things made sense!
To sum it all up, here's an example:
int a = 0;
refFunc(a); // Good. a now equals 1.
ptrFunc1(&a); // Good. a now equals 2.
ptrFunc2(&a); // Doesn't work. a still equals 2.
ptrFunc1(a); // This should be a compiler error.
refFunc(int &arg) {
arg = 1;
}
ptrFunc1(int *arg) {
*arg = 2;
}
ptrFunc2(int *arg) {
arg = 3; // Setting the value of the pointer, not the intended variable.
}
Make sense?