Which way is best/right? pointers and referances

When changing a variable using a function, when the variable is outside that function, using pointers, which way is best? They both seem to do the same thing, the same way, but i wanted to know the right way. The first way is quicker to write.

first way is to call a function change(int a)
and to receive change(int& x){ x = 7}

the second way is to call the function change(int &a)
and receive change(int *x){*x = 7}

both change a to 7.
I would probably use the first one because its easier and simpler to write out.
but i would love to know the answer
If you need to change an array, you shouldn't use references.
Otherwise, it's a matter of preference...
Many times, references are no more than hidden pointers, so there is really no difference in speed or memory usage.
That's what references are for. When in doubt, look at the standard library: from std::swap to std::getline, from std::advance to std::lock, whenever a function needs to change a parameter, it takes it by reference.

(Note, there is nothing special for the arrays, std:swap takes references to arrays)
Topic archived. No new replies allowed.