differences between a pointer and a


Differences between a pointer and a reference

1.  A reference must always point to some object where as this restriction is not imposed on a pointer.

e.g.

int *pi = 0;           // pointer can point to no object.

const int &ri = 0      would be converted as ,

int temp = 0;

const int &ri = temp;

2. The assignment of one references with other changes the object being referenced and not the reference itself.

e.g.

 int ival1 = 1000, ival2 = 2000;

 int *pi1 = &ival1, *pi2 = &ival2;

 int &ri1 = ival1, &ri2 = ival2;

pi1 = pi2;

 

  • ival1 remains unchanged but pi1 and pi2 now address the same object ival2.

ri1 = ri2;

  • ival1 becomes 2000. ri1 and ri2 still refer to ival1 and ival2 respectively.

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: differences between a pointer and a
Reference No:- TGS0309310

Expected delivery within 24 Hours