294 Programming and Data Structures
{
int a=25,b=10,*p,*j;
P-&a;
clrscrO ;
printf ("n Addition a+b = %d", *p+b);
printf (“
Subtraction a-b = %d", *p-b);
printf ( “
Product a*b = %d", *p**j);
printf ( “
Division aJb = %d", *p / *j);
printf ("n a Mod b = %d", *p % *j);
QUTFUT;
Addition a+b = 35
Subtraction a-b = 15
Product a*b = 250
Division a/b = 2
a mod b = 5
Explanation The various arithmetic operations can be performed on a pointer such as addition,
subtraction, multiplication, division, and mod. Here, the value stored at the address is taken into
account for operations but not the address. The arithmetic operations are impossible with addresses.
For example, two address locations are not possible to add.
Various arithmetic operations performed with the above program are elaborated as under.
1) Addition A number can be added to a pointer or addition of two variables through pointers is also
possible. In the fir s t p r in t f () statement value of ' b 7 is added to pointer of ' a ' i.e. *p. The result of
addition is 35.
2) Subtraction A number can be subtracted from a pointer. Subtraction of two variables through
pointers is also possible. In the second p r i n t f () statement value of ' b ' is subtracted from pointer of
' a ' i.e. *p. The result of subtraction is 15.
3 M ultiplication Multiplication of two pointers or a multiplication of number with pointer variable
can be done. In the third p rin tfO statement multiplication of variable x a ' and 1 b ' is done through
their pointers ' * p # and % * j #.
Similarly, division and m od operations can be carried out as shown in the above program.
Please note that the following operations with addresses are not possible.
1) Addition of two addresses (pointers).
2) Multiplication of addresses or multiplication of address with a constant.
3) Division of address with a constant.
9.14 Write a program to compare two pointers. Display message "Two pointer have same
address " or 'T w o pointer have different addresses".
# inclu d e < s td io .h >
# in clu d e <co n io.h >