Professional Documents
Culture Documents
College of Informatics Department Computing and Engineering Fundamental Programming 2
College of Informatics Department Computing and Engineering Fundamental Programming 2
College of Informatics
Department Computing and Engineering
Fundamental Programming 2
Pointer
ptr = &Var;
//Ptr now holds the address of the variable ’var’.
E.g. // demo of the reference operator
//this prog prints the addresses of the three variables.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int var1=10,var2=20,var3=30;
cout<<endl<<&var1<<endl<<&var2<<endl<<&var3;
return 0;
}
Pointers are said to "point to" the variable whose reference they
store. Using a pointer we can access the value stored in the
variable which it points to. To do this, we simply have to precede
the pointer's identifier with an asterisk (*), which acts as
dereference operator and that can be literally translated to "value
pointed by". Therefore, following with the values of the previous
example, if we write:
// Value = *Ptr;
read as: "Value equals to value pointed by Ptr"
N.B Ptr refers to the address location( say 1776), while *Ptr (with
an asterisk * preceding the identifier) refers to the value stored at
address 1776. Notice the difference of including or not including
the dereference operator
KIOT@SE by Ashenafi Workie 7
Let see with example
// Value = *Ptr;
Ptr2 = Ptr; // Ptr2 equal to Ptr ( 1776 )
Ptr2 = *ptr; // Ptr2 equal to value pointed by ted (say 736 )
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int numbers[]={10,20,30,40,50};
for (int j = 0 ; j<5; j++)
cout<<* (numbers +j)<< endl; // displays the
contents one by one
return 0;
}
Questions