302 Programming and Data Structures
OR
9.24 Write a program to display array elements and their address. Use array name it self as
a pointer.
# include <stdio.h>
# include <conio.h>
void main ()
{
int i;
int a(]£3]-{{l,2,3},{4,5,6},{7,8,9}};
clrscrO;
printf ("tElements o f An Array with their addresses.nn ");
for (i=0,-i<9,i++)
printf ("%8u"Aa[0]l0}+i);
printf ( " [%d]",*(&a[0J[0]+i));
if(i==2 I I i==5)
printf (" n ");
}
}
Q V T E U T ;
Elements of An Array with their addresses.
1(4052] 2 [4054] 3 [4056]
4[4058] 5(4060] 6 [4062]
7 [4064] 8140661 9 [4068]
Explanation The logic of the program is same as the previous one. The only difference is that the
array name itself is used as a pointer. The i f statement inserts a line after displaying every three
elements.
9.7 ARRAY OF POINTERS
So far we have studied array of different standard data types such array of
in t, flo a t , characters
etc. In the same way the 'C ' language also supports array of pointers. It is nothing but a collection of
addresses. Here, we store address of variables for which we have to declare an array as a pointer.
9.25 Write a program to store addresses of different elements of an array-using array of
pointers.
# include <stdio.h>
# include <conio.h>
void main ()
{
int *arrp[3];
Pointers 303
int arr±[3] ={5,10,15},k;
for (k=0;k<3;k++)
arrp [k] =arrl+k;
clrscr();
printf ("ntAddress Elementri');
for (k=0;k<3;k++)
I
printf (J't%u"rarrp[kl);
printf ("t%7d n",*(arrp[k]));
I
QJUTTOT?
Address Element
4060 5
4062 10
4064 15
Explanation In the above program *arrp [3] is declared as an array of pointer. Using first fo r loop
addresses of various elements of array % ar r [ ] 7 are assigned to ' * arrp [ ] '. The second for loop
picks up the addresses from % *arrp [ ] ' and displays the value present at that locations. Here, each
element of x*arrp [ ] ' points to respective element of array %arr []
Table 9.2 Array of pointers in memory
Element no.
Array of values Element no.
Array of addresses
arr [0] 5
arrp[0]
4060
arr [1] 10
arxp[l]
4062
arr [2] 15
arrp [2]
4064
9.26 Write a program to display address of elements and pointers.
# include <stdio.h>
# include <conio.h>
void main ()
{
int a[5] ={0,1,2,3,4};
int *p[5], i;
for (i=0;i<5;i++)
p [i] =a+i;
clrscr();
..................Content has been hidden....................

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