Pointers
These are the variables that are used to
hold the address of another variable
Pointer variable
• A pointer is a variable that contains the memory location
of another variable.
• Syntax:-
• type * variable name
• You start by specifying the type of data stored in the
location identified by the pointer.
• The asterisk tells the compiler that you are creating a
pointer variable.
• Finally you give the name of the variable.
Declaration and Initialization
• Syntax for declaring pointer is
data_type * ptr variable name;
Example : int *p;
Syntax for Initialization of a pointer is
ptr var name= & var name;
Example : p=&a;
Declaring a Pointer Variable
To declare ptr as an integer pointer:
int *ptr;
To declare ptr as a character pointer:
char *ptr;
Address operator:
• Once we declare a pointer variable we must point it
to something we can do this by assigning to the
pointer the address of the variable you want to point
as in the following example:
ptr=#
• This places the address where num is stores into the
variable ptr. If num is stored in memory 21260
address then the variable ptr has the value 21260.
Address and Pointers
• Memory can be
conceptualized as a ADDR1 Contents1
ADDR2
linear set of data ADDR3
locations. ADDR4
ADDR5
• Variables reference the ADDR6
*
contents of a locations *
*
• Pointers have a value of
ADDR11 Contents11
the address of a given
location *
*
ADDR16 Contents16
Lect 14 P. 6
Pointer Variable
Assume ptr is a pointer variable and x is an integer variable
x 10
ptr &x
Now ptr can access the value of x.
HOW!!!!
x = 10
Write: *variable . For example:
ptr = &x Cout<< *ptr;
Program to display variable value and
address value
main ()
{
int *p, a;
Cout<<“enter the value of a”;
Cin>>a;
p=&a;
Cout<<“address of variable a using pointer is”<<p;
Cout<< value of variable a using pointer is”<<*p;
getch();
}
// variable address= p
// variable value=*p
Another Program
int a=20;
int *p;
p=&a;
Cout<<a<<&a;
Cout<<p<<&p<<*p;
P has an address 500
Variables, Addresses and Pointers
• main() • Memory Value
{ • a (1001) 5
int a = 5; • b (1003) 6
int b = 6;
• c (1005) 1001
int *c;
// c points to a
c = &a;
Cout<<a<<*c<<b;
}
Pointer to pointer
Pointer variable that holds the address of another
pointer
Declaration :
int **p1,*p;
Initialization
p1=&p;
Pointers to pointers
• A pointer variable containing address of another
pointer variable is called pointer to pointer
void main()
{
int a=2, *p, **q;
p=&a;
q=&p;
cout<<a<<“is stored at ”<<p<<“and pointer is
stored at ”<<q;}
The Relationship Between Arrays and
Pointers
• Array name is starting address of array
int a[] = {4, 7, 11};
4 7 11
starting address of vals: 0x4a00
cout << a; // displays
// 0x4a00
cout << a[0]; // displays 4
9-13
• int main()
• { float a[5];
• float *ptr;
• cout << "Displaying address using arrays: "<<endl;
• for (int i = 0; i < 5; ++i)
• { cout << &a[i] <<endl; }
• ptr = a; // ptr = &a[0]
• cout<<"\nDisplaying address using pointers: "<< endl;
• for (int i = 0; i < 5; ++i)
• { cout << ptr+i <<endl; } return 0; }
Display addresses using pointer
notation
• int main()
• { float a[5];
• cout<<"Displaying address using pointers notation:
"<< endl;
• for (int i = 0; i < 5; ++i)
• { cout << a+i <<endl; }
• return 0; }
• int p[5] = {3, 4, 5, 5, 3};
• &p[0] is equal to p and *p is equal to p[0]
• &p[1] is equal to p+1 and *(p+1) is equal to p[1]
• &p[2] is equal to p+2 and *(p+2) is equal to p[2]
• &p[i] is equal to p+i and *(p+i) is equal to p[i]
Assignment in pointers
• Pointer variables can be "assigned":
int *p1, *p2;
p2 = p1;
– Assigns one pointer to another
– "Make p2 point to where p1 points"
• Do not confuse with:
*p1 = *p2;
– Assigns "value pointed to" by p1, to "value
pointed to" by p2
Comparison in pointers
• Two pointers of the same type, p and q, may be
compared as long
• as both of them point to objects within a single memory
block
• • Pointers may be compared using the <, >, <=, >=, == , !=
• • When you are comparing two pointers, you are
comparing the
• values of those pointers rather than the contents of
memory locations pointed to by these pointers
Pointer Arithmetic
void main()
{
int a=25,b=78,sum;
int *x,*y;
x=&a;
y=&b;
sum= *x + *y;
cout<<“Sum is : ”<<sum;
}
Pointer Assignment
Pointer Arithmetic
Void main(){
Int *p,a,**p1;
Clrscr();
Cout<<“Enter the value of a”;
Cin>>a;
p=&a;
p1=&p;
Cout<<“address of variable a using ptr”<<p;
p=p+4;
Cout<<“modified address is”<<p;
Cout<<“value of variable a using ptr is”<<*p;
*p=*p+20;
Cout<<“modified value of variable a is ”<<*p;
Cout<<“address of pointer p is”<<p1;
getch();}
Void Pointer
• It is called the Generic pointer
• It is a special type of pointer that can be pointed at
objects of any data type
• A void pointer is declared like a normal pointer,
using the void keyword as the pointer’s type
• Example: void *pVoid; // pVoid is a void pointer
Void Pointer (Contd)
A void pointer can point to objects of any data type
Example:
main()
{ int i;
char c;
void *data;
i = 6;
c = 'a';
data = &i;
cout<<"the_data points to the integer value "<< *(int *)data;
data = &c;
cout<<"the_data now points to the character "<< *(char*) data;
getch();
}
Other Properties of Void Pointer
• It Can’t be Dereferenced because void pointer does
not know what type of object it is pointing to.
• It is not possible to do pointer arithmetic on a void
pointer.
• Since void pointers can’t be dereferenced, there is
no such thing as a void reference
Null Pointer
• A null pointer is generally used to signify that a
pointer does not point to any object
• NULL pointer is a type of pointer of any data type
and generally takes a value as zero. This is, however,
not mandatory. This denotes that NULL pointer
does not point to any valid memory address.
Example of Null Pointer
• int* ptr;
• ptr=0;
• The above statement denotes exforsys as an integer
pointer type that does not point to a valid memory
address. This shows that exforsys has a NULL pointer
value.
Difference between Null Pointer and Void
Pointer
A Void pointer is a special type of pointer of void and
denotes that it can point to any data type.
NULL pointers can take any pointer type, It means
pointer can’t point to anything .It resolves the
problem of dangling and wild pointers.
Dangling Pointers
• Points to Invalid Location
• Dangling pointers arise when an object is deleted or
de allocated, without modifying the value of the
pointer, so that the pointer still points to the
memory location of the de allocated memory.
Dangling Pointer
Difference between dangling Pointer and
Null Pointer
• NULL pointer points to nothing. But dangling
pointers are those pointers which points to invalid
location (e.g. still points to those memory locations
which are already freed)
int *p = NULL;
Is mere a pointer which is not initialised to valid
memory location. it points to nothing int *q ; q ->
0x1Ax
Pointer to Object
• Pointer points to class variable
• Functions are Accessed using -> operator
• Program
Pointers to Members
• It is possible to take address of a member of a class and assign
it to a pointer.
• A class member pointer can be declared using the operator ::*
with the class name.
Ex: class A
{
private:
int m;
public:
void show();
};
• Define a pointer to the member m as follows:
int A ::* ip = &A :: m;
– ip pointer created thus acts like a class member in
that it must be invoked with a class object.
– A::* means “pointer-to-member” of A class.
– &A::m means the “address of the m member of A
class”.
– ip now be used to access the member m inside
member functions.
• The dereferencing operator ->* is used to access a
member when we use pointers to both the object
and the member.
• The dereferencing operator .* is used when object
itself is used with the member pointer.
Example: pointer to data member and
member function
#include <iostream> // create an object of class type X
using namespace std;
X xobject;
class X
{ // initialize data member
public: xobject.*ptiptr = 10;
int a;
void f(int b) cout << "The value of a is " << xobject.*ptiptr<<
{
endl;
cout << "The value of b is "<< b
<< endl;
} // call member function
}; (xobject.*ptfptr) (20);
int main() }
{
// declare pointer to data member
int X::*ptiptr = &X::a;
// declare a pointer to member
function
void (X::* ptfptr) (int) = &X::f;
This pointer
• ‘this’ pointer is a pointer that holds the memory
address of the current object.
Example
class where main()
{ {
private: where w1, w2, w3;
char charray[10]; //make three objects
//occupies 10 bytes w1.reveal(); //see where
public: they are
void reveal() w2.reveal();
{ cout << “\nMy object’s w3.reveal();
address is “ << this; } cout << endl;
}; return 0;
}
class book
Another example
void address()
{ {
char name[20]; cout<<"address of
float price; object"<<this<<endl;
public: }
void get() };
{ int main()
{
cout<<"enter the value of name and book b1,b2;
price"; b1.get();
b2.address();
cin>>name>>price;
}
cout<<this<<endl;
cout<<"name"<<this->name<<endl;
//cout<<"price"<<(*this).price<<end
l;