Pointers 289
{
int i,*k ;
clrscr();
printf ("Enter a number: " );
scanf (" % d " M );
k=&ci;
printf ("nAddress ofi is % u ”,k);
printf ("nValue ofi is %d",*k);
OUTPUT:
Enter a number: 15
Address of i is 4062
Value of i is 15
Explanation In the above program the address of variable 1 i ' is assigned to pointer variable % k ' .
Hence,' k' is pointing to ' i '. Value of the variable ' i ' is displayed using the pointer *k.
9.8 Write a Program to add two numbers through variables & their pointers.
# include <stdio.h>
# include <conio.h>
void main ()
{
int a,b,c,d,*ap,*bp;
c lr sc r();
printf ("Enter Tivo Numbers
scanf ("% d % d " M A b );
ap^&ca;
bp=&b;
c=a+b;
d=*ap+*bp;
printf ("nSum of A & B Using Variable :% d ”,c);
printf ("nSum o f A & B Using Pointere :% d”,d);
OUTPUT;
Enter Two Numbers : 8 4
Sum of A & B Using Variable: 12
Sum of A & B Using Pointere: 12
Explanation In the above program sum of two numbers are obtained by two ways.
1. Adding variable ' a ' & xb r directly.
2. Through pointers of 'a ' & 'b ' i.e. 'ap' & 'bp' respectively.
290 Programming and Data Structures
Address of 'a / & 'b ' are stored in 'a p ' & %bp' respectively. Thus adding x*ap ' & '*b p ' gives
addition of 'a ' & 'b'.Here, 'a p ' & 'b p' means addresses of 'a ' & 'b' and ' *ap' & '*b p # means
the values at memory locations. Hence, the equation used here is d= * ap+*bp; & no t d=ap+bp.
9.9 Write a program to assign pointer value to another variable.
# include <stdio.h>
# include <conio.h>
malnQ
{
int a*5,b,*c;
c*&a;
b»*c;
clrsc rO ;
printf ( nMemory location of'a '= % u "c );
printf ("n The value ofa=%d&c b=%d",a,b);
I
Q i m i l L
Memory location of 'a'= 4062
The value of a=5 & b=5
Explanation In the above program, the pointer variable ' c ' is assigned in the memory location of
% a '. The value stored in this memory location is assigned to 'b ' through pointer *c.
9.10 Write a program to assign value of 'b ' to " through pointers. Show the effect of addition
before and after assignment of value of 'b ' to'a'.
# include <stdio.h>
# include <conio.h>
void main ()
{
in t a#b,*pa,*pb;
clrscr ();
printf ("Enter Value of'a'&c
scanf ("% d %d",&a,&b);
pa=6uz;
pb=&b;
printf ("nValue ofa=%d&a b=%d",a,b);
printf ( Address ofa=% u",pa);
printf ("nAddress ofb=% u ",pb);
printf ( nnAddition of'a'&z 'b ': % d",*pa+*pb);
pa=pb;
printf ( n *p a =% d *pb=%d",*pa,*pb);
printf ( pa =%u pb=%u",pa,pb);
printf ( nAddition of*pa + *p b : % d",*pa+*pb);
getcheO;
i
..................Content has been hidden....................

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