Pointers 301
9.6 POINTERS AND TWO-DIMENSIONAL ARRAYS
A matrix can represent two-dimensional elements of an array. Details about it are explained in the
chapter 9 ] Array. Here, the first argument is row number and second column number. To display the
elements of two-dimensional array using pointer it is essential to have ' &' operator as pre-fix with an
array name followed by element numbers, otherwise compiler shows an error.
9.23 Write a program to display array elements and their address using pointers.
# include <stdio.h>
# include <canio.h>
void main ()
{
int i, j= l,*p ;
int a [3 ][3 ]= {{1 ,2 ,3 },{4 ,5 ,6 },{7 ,8 ,9 }};
clrscr();
printf ("\tElements of An Array with their addresses\n\n");
p=ba[0][0];
for (i=0,i<9,i++,j++)
I
printf ("%5d [%5u ]",*(p),p);
P++;
if(j==3)
I
printf ( " W ');
j=0;
I
I
OUTPUT:
Elements of An Array with their addresses.
1 [40521 2 [4054] 3 [4056]
4 [4058] 5 [4060] 6 [4062]
7 [4064] 8 [4066] 9 [4068]
Explanation In the above program two-dimensional array is declared and initialized. The base
address of an array is assigned to integer pointer ' p ' . While assigning the base address of two-
dimensional array, operator is to .be pre-fixed with array name followed by element numbers
which are necessary otherwise the compiler shows an error. The statement p=&a [ 0 ] [ 0 ] is used in this
context. The pointer 'p ' is printed and increased in for loop till it prints entire array elements. The i f
statement splits a line when three elements in each row are printed.

Get Programming and Data Structures now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.