Primitive or struct type:

//eg.
some_type Some_Variable;

cout <<Some_Variable;  //value
cout <<&Some_Variable; //pointer
cout <<*Some_Variable; //ERROR!

Reference type:

//eg.
some_type& Some_Variable; //ERROR!
some_type& Some_Variable = Some_Other_Var; //OK, hard-linked

cout <<Some_Variable;  //value
cout <<&Some_Variable; //pointer
cout <<*Some_Variable; //ERROR!

Pointer type:

//eg.
some_type* Some_Variable;

cout <<Some_Variable;  //pointer
cout <<&Some_Variable; //pointer of pointer
cout <<*Some_Variable; //value

Function with pass-by-value parameters:

//values of A and B won't change
function swap(int A,int B) {
  int Temp=A; A=B; B=Temp;
}

//call the function
int A = 10;
int B = 20;
swap(A,B);

Function with pass-by-reference parameters:

//values of A and B are swapped
function swap(int& A,int& B) {
   int Temp=A; A=B; B=Temp;
}

//call the function 
int A = 10;
int B = 20;
swap(A,B); //NO AMPERSANDS BEFORE A AND B

Function with pass-by-pointer parameters:

//values of A and B are swapped
function swap(int* A,int* B) {
  int Temp=*A; *A=*B; *B=Temp;
}

//call the function (standard way for pointers)
int* A = new int();
int* B = new int();
*A = 10;
*B = 20;
swap(A,B);

//call the function (use reference)
int A = 10;
int B = 20;
swap(&A,&B);
Advertisements