304 Programming and Data Structures
for(i=0;'<5/'i++)
{
printf ("nt%d at location",*(*p+i));
printf ("t% u at location ",*(p+i));
printf ("%u",p+i);
I
printf ("nn Integer requires 2 bytes, address require 4 bytes");
O UTPU T;
Oat location 4036 at location 4046
1 at location 4038 at location 4050
2 at location 4040 at location 4054
3 at location 4042 at location 4058
4 at location 4044 at location 4062
Integer requires 2 bytes, address require 4 bytes
Explanation In the above program the first f o r loop assigns addresses of elements of an integer
array to a pointer array The f i r s t p r i n t f () statement prints element, second displays address of
element and the third displays address of the address i.e. address of the pointer. Thus, it is clear from
the above example that an integer requires two b y tes and pointer requires fou r bytes.
9.8 POINTERS TO POINTERS
Pointer is known as a variable containing address of another variable. The pointer variables also have
an address. The pointer variable containing address of another pointer variables is called as pointer
to pointer. This chain can be continued to any extent.
The program given below illustrates the concept of pointer to pointer.
9.27 Write a program to print value of a variable through pointer and pointer to pointer.
# include <stdio.h>
# include <conio.h>
void main ()
{
int a«2,*p,**q;
p=&a;
q**p;
clrscrO ;
printf ("n Value of a - %d Address of a=%u",a,$x.a);
printf ("n Through *p Value of a - %d Address ofa=%u",*p,p);
printf ("n Through **q Value of a - %d Address ofa=%d",**q,*q);
Pointers 305
OUTPUT:
Value of a= 2 Address of a=4056
Through *p Value of a= 2 Address of a=4056
Through **q Value of a= 2 Address of a=4056
Explanation In the above program.variable 'p ' is declared as a pointer. The variable 'q ' is declared
as pointer to another pointer. Hence, they are declared a s ' *p' and '* * q ' respectively. The address of
variable %a' is assigned to pointer 'p'. The address of pointer 'p ' is assigned to xq'. The variable 'q #
contains address of a pointer variable. Hence, 1 q ' is pointer to pointer. The program displays value
and address of variable 'a ' using variable itself and pointers 'p ' and %q '. The Table 9.3 illustrates
the contents of different variables used in the program
Table 9.3 Pointer to pointer
Variable
Pointer ( * )
Pointer of pointer (* *)
a
P
q
2
4056
4058
9.28 Write a program to use different levels of array of pointer to pointer and display the
elements.
# Include <stdio.h>
# Include <conio.h>
void main ()
{
int k;
int a[]e>{l,2,3};
Int *b [3] >
Int **c [3 ];
int ***d [3 ];
int * * * * » [3];
int [3];
clrsc rO ;
for (k=0;k<3;k++)
{
b[k]=a+k;
c[k]=b+k;
d[k]=c+k;
e[k]=d+k;
flk]=e+k;
I
for (k=0;k<3;k++)
I
printf ("% 3 d"*b[k ]);
printf ("% 3 d "**c[k]);
printf r ° / o 3d "* * * d [k ]);
printf ("%3d",****e[k]);
printf r%3dn"*****flk]);
..................Content has been hidden....................

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