FAQ Pointer & reference (C++) [Archive] - C Board

PDA

View Full Version : FAQ Pointer & reference (C++)


ayesha
10-28-2001, 02:30 PM
What is the difference between using a pointer and using a reference?
e.g.:

int K;
int*p=&k ;
&ref=k;
(both *p=20; and ref=20; will assign the value 20 into the variable k)

thanks for ur time and help.
~ayesha~

rc7j
10-28-2001, 04:23 PM
The "Cliff Notes" version of your question is a reference is a pointer but with less versatility. The reference is simpler and easier to use. A reference variable cannot be altered to refer to any variable except the one to which it was initialized.
A reference passed to another function is preferred if possible because while the receiving function has a copy of the value in the address passed, it cannot change the value in the original function. Thus protecting the original value.
References cannot be used for array notation.