Pointers 291
OUTPUT;
Enter Value of V & V . : 8 4
Address of a= 4056
Address of b= 4058
Addition of 'a' & /b ': 12
*pa=4 *pb=4
pa= 4056 pb=24350
Addition of *pa & *pb : 8
Explanation In the above program address of variable ' a ' and 'b ' are assigned to the pointer ' pa'
and ' pb' . The statement pa=pb; means that the value of pointer 1 pb# is assigned to pointer % pa9.
Now, both the pointers have the same values and they point to the same variable 'b9. Hence, the
addition is 8.
9.4 ARITHMETIC OPERATIONS WITH POINTERS
Arithmetic operations on pointer variables are also possible. Increase, decrease, prefix, & postfix
operations can be performed with the helpf of the pointers. The effect of these operations are shown in
the below given Table 9.1.
Table 9.1 Pointer and Arithmetic Operation
Data Type Initial
Address
Operation Address
After
Operations
Required
Bytes
int i=2 4046
++
4048
4044 2
char c='x'
4053
++
4054 4053
1
float f=2.2 4058
++
4062
4054
4
long 1=2
4060
++
4064
4056
4
From the above table w e can observe that on increase of the pointer variable for integers the address is
increased by two i.e. 4 04 6 is original address and on increase it's value will be 4 04 8 because integers
require two bytes.
Similarly, for characters, floating point numbers and long integers requires 1, 4 and 4 bytes respectively.
After the effect of increasing and decreasing the memory locations are shown in the Table 9.1.
9.11 Write a program to show the effect of increment on pointers variables. Display the
memory locations of integer, character, and floating point numbers before and after
increment of pointers.
# Include <stdio.h>
# include <conio.h>
void main ()
{
292 Programming and Data Structures
int x, *xl;
char y, *yl;
float z,*zl;
clrscrO ;
printf ("Enter integer, character, float Values n");
scanf ("% d %c %
xl=&x;
yl=&y;
zl=&z;
printf ("Address of x = %un”,xl);
printf ("Address o fy = %un",yl);
printf ("Address of c = % un",zl);
xl++;
yl++;
zl++;
printf ("nAfter Increment in Pointersn");
printf ("nN ow Address ofx=%un"jcl);
printf ("N o w Address of y=%un",yl);
printf ("N o w Address ofz=%un",zl);
printf ("nSize of Integer : %d",sizeof(x));
printf ("nSize of Character: % d",sizeof(y));
printf ("nSize of Float : % d, sizeof(z));
1
OUTPUT:
Enter integer, character, float Values
2 A 2.2
Address of x = 4046
Address of Y = 4053
Address of Z = 4058
After Increment in Pointers
Now Address of x = 4048
Now Address of y = 4054
Now Address of z = 4062
Size of Integer : 2
Size of Character: 1
Size of Float :4
Explanation Observe the output. 4 0 4 6 is the address of an integer ' x 7,4 0 5 3 is address character
' y #, and 4 0 5 8 is the address of the floating point number ' z '. On increase of pointer the address of
integer, character and float will be 4048,4054&4062 respectively. This is because every time a pointer
points immediately next to or a previous location of its type after the increase or decrease in the
operation respectively.
Pointers 293
9.12 Write a program to show the effect of incrementation and decrementation operators used as
prefix and suffix with pointer variable.
# include <stdio.h>
# include <canio.h>
void main ()
{
int i, *ii;
puts ("Enter Value of is");
scanf (*%d",&i);
ii=*&i;
clrscr();
printf ("Address ofi = %un",ii);
printf ("Address ofi = %iAh"/++ii);
printf ("Address ofi = % iA h " «++);
printf ("Address ofi = %un",~ii);
printf ("Address ofi = %un",ii~);
printf ("Address ofi = %un",ii);
QXJTEUY:
Enter Value of
i= 8Address of
i = 4060Address of
i = 4062Address of
i = 4062Address of
i = 4062Address of
i = 4062Address of i = 4060
Explanation
1) In the 1st p rin tf () statement displays the address of i i =4060.
2) In the 2nd p r in tf () statement prefix is increased with a pointer variable %i i ' , so before
printing it is increased. Hence, pointer is an integer type, an integer requires two bytes in
memory. Hence, the address ofii=4062.
3) The 3rd p r in tf () statement is opposite to the 2nd. Here, the address value is printed first
and then it is increased. Hence, it prints ii=4062 and after printing the increased address
becomes 4064.
4) In 4th p r in tf () statement address of 1 i ' is decreased first and then printed. Hence address
of *i'is4062.
5) In 5th p rin tf () statement address of x i ' is printed first and then decreased. On decreasing
address o f ' i ' becomes 4060.
6) The last prin t f () statement prints the initial address of ' i '.
9.13 Write a program to perform different arithmetic operations using pointers.
# include <stdio.h>
# include <conio.h>
void main()
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 >
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.186.178