Pointer Programs
Pointer Programs
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
int *p,*q;
clrscr();
p=&a;
q=&b;
printf("Address of a is %d and its value is %d\n",p,*p);
printf("Address of b is %d and its value is %d\n",q,*q);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*p1,*p2,x,y,z;
clrscr();
a=12 ;
b=4 ;
p1=&a ;
p2=&b ;
x=*p1 * *p2 - 6 ;
y= 4 * - *p2 / *p1 +10 ;
printf( “Address of a = %d\n”,p1);
printf( “Address of b = %d\n”,p2);
printf(“a=%d, b=%d\n”,a,b);
printf(“x=%d, y=%d\n”,x,y);
*p2 = *p2 + 3;
*p1 = *p2-5;
z = *p1 * *p2 -6;
printf(“a=%d, b=%d\n”,a,b);
printf(“z=%d\n”,z);
}
Output:
Address of a = -108
Address of b = -110
a=12, b=4
x=42, y=9
a=2, b=7
z=8
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]={1,2,3,4,5};
int *p=x,i,j,n;
clrscr();
for(i=0;i<5;i++)
{
printf("Address of x[%d] is %d and its value is %d\n",i,p,*p);
p++;
}
getch();
}
Output:
Address of x[0] is -20 and its value is 1
Address of x[1] is -18 and its value is 2
Address of x[2] is -16 and its value is 3
Address of x[3] is -14 and its value is 4
Address of x[4] is -12 and its value is 5
#include<stdio.h>
#include<conio.h>
void main()
{
int x[3][4]={1,2,3,4,5,6,4,5,6,7,8,9};
int *p=x,i,j,n;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<4;j++)
{
printf("Address of x[%d][%d] is %d and its value is %d\n",i,j,p,*p);
p++;
}
getch();
}
Output: