Suppose that we desire to swap the contents of two integer variables. Consider the following function:
void swap(int v1, int v2) { int tmp=v2; v2=v1; v1=tmp; } main() { int i=10; int j=20; cout << "Before swap():\ti:"<<i<<"\tj:"<<j<<endl; swap(i,j); cout << "After swap():\ti:"<<i<<"\tj:"<<j<<endl; }when executed the result is not as we desired, but it isBefore swap(): i:10 j:20 Two alternatives to solve the problem: The first alternative is to use pointers as parameters:
After swap(): i:10 j:20
void pswap(int *v1, int *v2) // parameters are pointers { int tmp=*v2; *v2=*v1; *v1=tmp; } main () {..... pswap(&i, &j); // send address of i and j as parameter ... }When executed, we will have:Before swap(): i:10 j:20 Second alternative is to use reference:
After swap(): i:20 j:10
void rswap(int &v1, int &v2) { int tmp=v2; v2=v1; v1=tmp; } main () {..... rswap(i, j); ... }When compiled and executed, we will haveBefore swap(): i:10 j:20 However if it is declared as
After swap(): i:20 j:10
void crswap(const int &v1, const int &v2) { int tmp=v2; v2=v1; v1=tmp; } main () {..... crswap(i, j); ... }When executed, we will haveBefore swap(): i:10 j:20 Pointer Types:
After swap(): i:10 j:20
A pointer variable holds values that are the addresses of objects in memory. Through a pointer an object can be referenced directly. Typical uses of pointers are the creation of linked lists and the management of objects allocated during execution.int* ip // pointer decleration Reference Types:All the operations applied to a reference act on the object to which it refers
int *ip // the same as above
int *ip1, *ip2 // two pointers
int* ip1, ip2 // a pointer, an integer
int ip, *ip2 // an integer and an integer pointer
long *lp, lp2 // a long integer and a long integer pointer
float fp, *fp2 // a floating point and a floating point pointer
int i=1024
//create an pointer that points to i
int *ip=i // error, type mismatch
int *ip=&i // ok. the operator & is referred as address-of operator
// create another pointer that also points to i
int *ip2=ip // ok. now ip2 also addresses i
// to create a pointer that points ip2
int *ip3=&ip2 // error, type mismatch
int **ip3=&ip2 // ok , it is a pointer to a pointer.Note that char is 1,int 4 and double is //8 bytes long
int i=1024
int *ip=&i; //ip points to i
int k=ip; //error
int k=*ip; // k now contains 1024
int *ip=… // type decleration for the pointer and initialization
*ip= … // the item pointed by ip
…=*ip // the item pointed by ip
=ip // the pointer itself
int k=-5
int *ip=&i;
*ip=k; // i=k
*ip=abs( *ip) // i=abs(i);
ip=ip+1//ip points to an integer that is 4 bytes so increases the value of the address it //contains by the size of the object, so it is incremented by 4
//in some compilers ip+1*size of (int)
int i,j,k;
int *ip=&i;
*ip= *ip+2 // add two to i, that is i=i+2
ip=ip+2; // add two to the address ip contains.In some compilers ip +2*sizeof(int)
int val=l0 demonstrate reference as;
int &refval=val
int *pi=&refval //initializes pi with the address of val
int *&refpi=pi // refpi is a reference to a pointer
int &*ptr=&refval //USELESS
// ptr is a pointer to a reference,
//however the address of a reference is same as the referenced variable
//therefore instead &*ptr simply use *ptr as below
int *ptr=&refval
A pointer argument can also be declared as a reference when the programmer wish to modify the pointer itself rather than the object addressed by the pointervoid prswap(int *&v1, int *&v2) //NOTE: *&v1 should be read from right to left v1 is a reference to a pointer to // an object of type int { int *tmp=v2; v2=v1; v1=tmp; } main() { int i=10; int j=20; int *pi=&i; int *pj=&j; cout << "Before swap():*pi:"<<*pi<<"*pj:"<<*pj<<endl; prswap(pi,pj); cout << "After swap(): i:"<<*pi<<"j:"<<*pj<<endl; }
When compiled and executed, we will haveBefore swap():*pi:10 j:20 Notes:
After swap(): *pj:20 j:10
prswap (pi,pj)
i
j
pi
pj
before swap *pi: 10 *pj: 20
after swap *pi: 20 *pj:10
const int* p; //pointer to a constant p=… OK *p= not OK int * const p; // a constant pointer p=… not OK *p=… OK const int* const p; // co constant pointer to a constant p=… not OK *p=… not OK const int &p; // reference to a constant p=… not OK int & const p; // constant reference // useless, a reference is already a constant, you can not change shortcut
No comments:
Post a Comment