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~
This is a discussion on FAQ Pointer & reference (C++) within the FAQ Board forums, part of the General Programming Boards category; What is the difference between using a pointer and using a reference? e.g.: int K; int*p=&k ; &ref=k; (both *p=20; ...
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~
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.
rc7j