Pointers 295
void mai n ()
{
int a=2,*j,*k;
j=&a;
k»&a;
clrscrO;
if (j = k )
printf ("n The Two Pointers have the same address.");
else
printf ("n The Pointers have the different address.");
printf ( Address ofa(j)=%u",j);
printf ( Address ofa(k)=%u",k);
I
OUTPUT
The Two Pointers have the same address.
Address of a(j)= 4056
Address of a(k)= 4056
Explanation The comparison between the two pointers is done in the above program. The pointer
variable should be of the same data type while comparison is made. The comparison of pointers can
test whether the addresses are identical or different. Here, in this program pointer' j ' and ' k' are of
integer data types and points to the same integer variable ' a '. Hence, they contain the same address.
9.5 POINTERS AND ARRAYS
Array name itself is an address or pointer. It points to the address of the first element (0,h) element of an
array). The elements of the array together with their addresses can be displayed by using array name
itself. Array elements are always stored in contiguous memory locations.
Programs in this regard are explained below.
9.15 Write a program to display elements of an array. Start element counting from 1 instead
of 0.
# include <stdio.h>
# include <conio.h>
void main()
int x[]={2,4,6,8,10),k=l;
clrscr();
while (k<=5)
I
printf ("%3d",k[x-l]);
k++;
296 Programming and Data Structures
}
OUTPUT:
2 4 6 8 10
Explanation Array elements counting always starts from 'O '. The element number is added in the
base address and each element of an array is accessed. If one is subtracted from the base address of an
array, it points to the prior address of 0th element. By adding one to its reduced base address it is
possible to start element counting from ' 1'.
9.16 Write a program to display array element with their addresses using array name as a
pointer.
# include <stdio.h>
# include <conio.h>
void main ()
{
int x[5 ]s{2,4,6,8 ,10},k=0;
clrscrO ;
printf (" Element No. Element Address");
while (k<5)
I
printf ("nx[% d] = %8d %9u",k,*(x+k)j+k);
/c++;
I
OUTPUT:
Element No. Element Address
x[0l 2 4056
x[l] 4 4058
x[2] 6 4060
xl3] 8 4062
x[4] 10 4064
Explanation In the above program variable ' k ' acts as an element number and its value varies from
0 to 4. When it is added with an array name 1 x ' i.e. with address of the first element, it points to the
consecutive memory location. Thus, element no., element, and their addresses are displayed.
OR
9.17 Write a program to display array elements with their addresses using array name as a
pointer.
# include <stdio.h>
# include <conio.h>
Pointers 297
void main ()
{
int mnn[4]={l0,25,35,45},i;
clrscr() ;
printf ("Element Address ”);
for (i=0;i<4,-i++)
I
prititf("mim[%d]=%d",i,*(num+i));
printf ("%8u n ,num+i);
I
OUTPUT:
Element Address
num[0] = 10 4062
num[l] = 25 4064
num[2] = 35 4066
num[3] = 45 4068
Explanation Here, in the above program the array name ' num' itself acts as a pointer to the array num
[ ]. The pointer % num' provides address of the first array element and ' *num' gives value stored at
that address. When ' i ' is added with ' num' , the equations * (num+i) and num+i show ' i ' the
element and its location respectively.
9.18 Write a program to access elements of array through different ways using pointer.
# include <stdio.h>
# include <canio.h>
void main()
{
int a r r[5]«{10,20,30,40,50},p=0;
clrscr();
for (p=0,-p<5,-p++)
{
printf ( Value ofarr[%d]=",p);
printf("%d I ",arr[p]);
printf ("% d I " *(arr+p));
printf ( “%d I ",*(p+arr));
printf ( %d I ",p[arr]);
printf ( address ofarrl%d]=%un",p,&:arr[p] );
I
OUTPUT:
Value of arr[0J=10 I 10 I 10 I 10 I address of arr[0]=4056
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.
Pointers 299
9.20 Write a program to display sum of squares and cubes of array elements using pointer.
# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
int b []> {l,2 ,3 ,4 ,5 }, j,sumsqteO/sumc«iO;
clrscr ();
for (j=0,*j<5;j++)
I
sumsq=sumsq+pow(*(j+b),2);
sumc=sumc+pow(b[j);
I
printf (" Sutrt of Squares of array elements: % d, sumsq);
printf ("nSum of Cubes of array elements: %d"fsumc);
}
Q u m m
Sum of Squares of array elements : 55
Sum of Cubes of array elements : 225
Explanation In the above program using pow () function square and cube of an array elements are
computed and added to variables ' sumsq' and 'sumc' respectively Usingprintf () statement sum of
square and cube of an array elements are displayed.
9.21 Write a program to copy element of one array to another array using pointers.
# include <stdio.h>
# include <ccnio.h>
void main ()
{
int so[]={l0,20,30,40,50},*pb,ds[5 ], i;
pb=so;
clrscr();
for (i=0,i<5,n++)
I
ds[i]=*pb;
pb++;
I
printf ("Orignal Array Duplicated Array");
for (i=0,n<5rf++)
..................Content has been hidden....................

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