298 Programming and Data Structures
Value of arr[l]=20 I 20 I 20 I 20 I address of arr[l]=4058
Value of arr[2]=30 I 30 I 30 I 30 I address of arr[2]=4060
Value of arr[3]=40 I 40 I 40 I 40 I address of arr[3]=4062
Value of arr[4]=50 I 50 I 50 I 50 I address of arr[4]=4064
Explanation In the above program the elements are displayed using different syntaxes,
a) arr [p] b) * (arr+p)) c) * (p+arr)) d) p [a r r ]) . The results of all of them would be the same.
a) arr [p] This statement displays various array elements. Here,' a r r' refers to the address and ' p'
refers to the element number.
b) * (arr+p) The arr+p is addition of a constant with the base address of an array. It shows the
address of pth element. The * (arr+p) points to thepth element of an array.
c) * (p+arr) This statement is same as (b ).
d )p [a r r ] This statement is same as (a ).Here, 'p ' refers to the element number and 'a r r ' refersto
the base address. By varying ' p f and ' a r r ' the various elements of the array are displayed.
9.19 Write a program to find sum of all the elements of an array. Use array name itself as a
pointer.
# include <stdio.h>
# include <conio.h>
void main ()
{
int sum?:0,i=0,a[]={l,2,3,4,5};
clrscrO ;
printf ("Elements Values Addressnn");
white (i<5)
I
printf ("al%d]t%5dt%8un",i,*(a+i),(a+i));
sum=sum+*(a+i++);
f
printf ("
Sum of Array Elements = %d",sum);
I
GUI PUT:
Elements Values Address
a[0] 1 4056
a[l] 2 4058
a[2] 3 4060
a[3] 4 4062
a[4] 5 4064
Sum of Array Elements = 15
Explanation In this program array name ' a ' acts as a pointer and variable T is used for referring
element numbers. Using the f o r loop and expressions * ( a + i ) & (a + i) various elements and their
addresses are displayed respectively. In the ' sum' variable sum of all the elements is obtained.