Pointers

Introduction to Pointers

As we all know every variable in C++ has a name and a value.So when we declare a variable , a specific block of memory is allotted to hold its value.Also the size of that variable depends upon the data type  of that variable.


for ex: int a=10;

here variable a is of integer data type and its value is 10 .


Pointer: Pointer is a variable that points to the address of another variable.

OR

Pointer is a derived datatype that refers to another data variable by storing the variable memory address.

In short purpose of pointer variable is to hold the address of any variable

 

Declaration of pointer:

data_type *ptr;

for ex: 1)int *ptr;

2)char *ptr1;

3)float *ptr2;

Here in first case ptr is a pointer of Int datatype ,it means it will be holding address of integer variable only.

In second case ptr1 is a pointer of char type,it will be holding address of char type.similarly in third case ptr2 is a pointer of float type and will hold address of float variable.

Important point to remember:

The datatype of the pointer variable and the variable to which pointer points should be same.


for ex:

int x =20;

int *ptr;

ptr=&x;


In above example

1)  *  indicate that ptr  is a pointer variable.

2) ptr is a name of pointer variable,  int specifies that pointer will be storing address of int datatype.

3).ptr=&x means pointer is assigned to address of x. or ptr variable will now have address of x.

Basically there are two main concepts related to pointer.

1.Referencing

2.Dereferencing

1.Referencing:Referencing simply means referencing the pointer to address of another variable.It can be done using Ampersad(&) sign

ptr=&x;

as told you above,assigning a pointer address of variable x. Here ptr will have address of c variable.

2.Dereferencing(Indirection operator): It means refering to the value of a variable to which it points by using(*)asterisk sign.It is also called as Indirection operator.

Returns the value of the variable that it points to.

*ptr=a;

Here ptr will have value of  x variable,which is called dereference,which means it extracts value ,when *ptr is used.

PROGRAM: A program to display address and value of a variable using pointer:

#include<iostream>
using namespace std;
main()
{int a=10,*ptr1;
ptr1=&a;
cout<<"the value of a:"<<a<<endl;
cout<<"the add  of a:"<<&a<<endl;

cout<<"the address of a:"<<ptr1<<endl;
cout<<"the value  of a:"<<*ptr1<<endl;
return 0;
}

PROPERTIES(RULES) OF POINTERS

1.A pointer variable can be assigned the address of another variable.

2.A pointer variable can be assigned the value  of another variable.

3.Pointer can be initialized with a NULL (0)value.

4.Pointers can be Incremented and Decremented.

5.Any integer value can be added or subtracted  from a pointer variable.

6.A pointer variable cannot be multiplied by a constant number.

7.A pointer variable can be compared by another pointer variable using relational operator.

RULES OF POINTER

Wrong methods or What should not be done.


CORRECT METHOD

int x;

int *p=&x;           // Assigned address of x to pointer during declaration.


If during Declaration of pointer variable  you are assigning pointer variable address of x, here * will not act as a dereference operator. so its right when done in declaration part.



WRONG METHOD

int x;

int *ptr;

*ptr=&x;


Above we  declared pointer variable and after that  address of x is  assigned to it.This is wrong,as here (*) indicates indirection.where you cant assign address of x to *ptr.

*ptr=x;    //it is the right way as *ptr can take value not address.



WRONG METHOD

ptr=x;

This is wrong way as here ptr variable when used directly without (*)asterisk  can be assigned address not value.



WRONG METHOD
int *p;
p=5678;     //address cannot be assigned directly by the user.


OPERATIONS ON POINTER

  • Arithmetic operations can be performed on pointers

–Increment/decrement pointer  (++ or –)

–Add an integer to a pointer( + or += , – or -=)

–Pointers may be subtracted from each other

  • Operations are meaningless unless performed on an array.

PROGRAM: program in C++ to Increment and decrement  pointer

//to display the address of pointer before and after incrementing and decrementing
#include<iostream>
using namespace std;
int main()
{int a=10,b=20,*ptr,*ptr1;
ptr=&a;
cout<<"Address of pointer before incrementing"<<ptr<<endl;
ptr++;
cout<<"Address of pointer after incrementing"<<ptr<<endl;
ptr1=&b;
cout<<"Address of pointer before decrementing"<<ptr1<<endl;
ptr1--;
cout<<"Address of pointer after decrementing"<<ptr1<<endl;
return 0;
}

OUTPUT

Address of pointer before Incrementing 0x6ffe3c

Address of pointer after decrementing 0x6ffe40

Address of pointer before Incrementing 0x6ffe38

Address of pointer before Incrementing 0x6ffe34.


so here when we used increment and  decrement operator on pointer variable address of pointer is incremented or decremented ,pointer variable is of int data type ,address will be incremented by two bytes as seen in result.

ARITHMETIC OPERATION PERFORMED ON POINTERS.

PROGRAM: program in C++ of arithmetic operations performed on pointer.

//arithmetic operators with pointers
#include<iostream>
using namespace std;
int main()
{int a=10,*ptr1,*ptr2,*ptr3;
ptr1=&a;
cout<<"Address of pointer ptr1= "<<ptr1<<endl;
ptr2=ptr1+2;
cout<<"Address of pointer ptr2= "<<ptr2<<endl;
ptr3=ptr1-2;
cout<<"Address of pointer ptr3= "<<ptr3<<endl;
return 0;
}

OUTPUT

Address of pointer ptr1= 0x6ffe34

Address of pointer ptr2= 0x6ffe3c

Address of pointer ptr3= 0x6ffe2c


As shown in result   addition and subtraction arithmetic operation when performed on pointer also increments or decrements the address respectively.But the use of arithmetic operator is of best use when used with arrays.


PROGRAM2:Example of arithmetic operation performed on Pointer.

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int num1=20 , num2=10, *ptr1 ,*ptr2 , sum, sub, mul, divide;

ptr1 = &num1 ;
ptr2 = &num2 ;
sum=*ptr1+*ptr2;
sub=*ptr1-*ptr2;
mul=*ptr1 * *ptr2;
divide=*ptr1 / *ptr2;

cout<<sum<<endl;
cout<<sub<<endl;
cout<<mul<<endl;
cout<<divide;
getch();
return(0);
}

 

 

Leave a Comment