12.Pointers
12.Pointers
A pointer is a memory variable that stores a memory address. Pointer can have any name
that is legal for other variable and it is declared in the same fashion like other variables
but it is always denoted by ‘*’ operator.
Features of pointers
Pointer declaration
int *x;
float *f;
char *y;
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf(“\n enter a number :”);
scanf(“%d”,&num);
printf(“\n value of num=%d\n”,num);
printf(“address of num=%u”,&num);
getche();
}
WAP to display the addresses of different variables together with their values.
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
int i;
float f;
clrscr();
printf(“\n enter alphabet, number, float=”);
scanf(“%c%d%f”,&c,&i,&f);
printf(“\n address of (c) %c=%u”,c,&c);
printf(“\n address of (i) %d=%u”,i,&i);
printf(“\n address of(f) %.2f=%u”,f,&f);
getche();
}
WAP to show pointer of any data type that occupies same space.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\t char %d byte & its pointer %d bytes\n”, sizeof(char),sizeof(char*));
printf(“\t int %d byte & its pointer %d bytes\n”, sizeof(int),sizeof(int*));
printf(“\t float %d byte & its pointer %d bytes\n”, sizeof(float),sizeof(float*));
getche();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int v=10,*p;
clrscr();
p=&v;
printf(“\n address of v=%u”,p);
printf(“\n value of v=%d”,*p);
printf(“\n address of p=%u”,&p);
getche();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int v=10,*p;
clrscr();
p=&v;
printf(“\n v=%d v=%d”,v,*(&v),*p);
getch();
}
WAP to add two numbers through variables & their pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,*ap,*bp;
clrscr();
printf(“\n Enter two number :”);
scanf(“%d%d”,&a,&b);
ap=&a;
bp=&b;
c=a+b;
d=*ap+*bp;
printf(“\n sum of a & b using variable :%d”,c);
printf(“\n sum of a & b using pointer :%d”,d);
getche();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b,*c;
c=&a;
b=*c;
clrscr();
printf(“\n memory location of ‘a’=%u”,c);
printf(“the value of a=%d” & b=%d”,a,b);
getche();
}
WAP to assign value of ‘b’ 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()
{
int a,b,*pa,*pb;
clrscr();
printf(“enter value of ‘a’ & ‘b’ :”);
scanf(“%d%d”,&a,&b);
pa=&a;
pb=&b;
printf(“\n value of a=%d & b=%d”,a,b);
printf(“\n address of a=%u”,pa);
printf(“\n address of b=%u”,pb);
printf(“\n addition of ‘a’ & ‘b’ : %d”,*pa + *pb);
pa=pb;
printf(“\n *pa=%d *pb=%d”,*pa,*pb);
printf(“\n pa=%u pb=%u”,pa,pb);
printf(“\n addition of *pa + *pb : %d”,*pa + *pb);
getche();
}
Arithmetic operations with pointers
Arithmetic operations on pointer variables are also possible. Increase, decrease, prefix &
postfix operations can be performed with the help of the pointers.
WAP to show the effect of increment on pointers variables. Display the memory
locations of integer, character, and floating point numbers before and after
increment of pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,*x1;
char y,*y1;
float z,*z1;
clrscr();
printf(“\n enter integer, character, float values\n :”);
scanf(“%d%c%f”,&x,&y,&z);
x1=&x;
y1=&y;
z1=&z;
printf(“\n address of x=%u”,x1);
printf(“\n address of y=%u”,y1);
printf(“\n address of c=%u”,z1);
x1++;
y1++;
z1++;
printf(“\n After Increment in Pointers”);
printf(“\n Now address of x=%u”,x1);
printf(“\n Now address of y=%u”,y1);
printf(“\n Now address of z=%u”,z1);
printf(“\n Size of Integer :%d”,sizeof(x));
printf(“\n size of character :%d”,sizeof(y));
printf(“\n size of float :%d”,sizeof(z));
getche();
}
WAP to show the effect of increased and decreased operator used as prefix and
suffix with pointer variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,*ii;
clrscr();
printf(“\n enter value of i=”);
scanf(“%d”,&i);
ii=&i;
clrscr();
printf(“address of i=%u\n”,ii);
printf(“address of i=%u\n”,++ii);
printf(“address of i=%u\n”,ii++);
printf(“address of i=%u\n”,--ii);
printf(“address of i=%u\n”,ii--);
printf(“address of i=%u\n”,ii);
getche();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a=25,b=10,*p,*j;
p=&a;
j=&b;
clrscr();
printf(“\n addition a+b=%d”,*p+b);
printf(“\n subtraction a-b=%d”,*p-b);
printf(“\n product a*b=%d”,*p**j);
printf(“\n division a/b=%d”,*p/*j);
printf(“\n a mod b=%d”,*p%*j);
getche();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int x[]={2,4,6,8,10},k=1;
clrscr();
while(k<=5)
{
printf(“%3d”,k[x-1]);
k++;
}
}
WAP 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[]={1,2,3,4,5};
clrscr();
printf(“elements values address\n\n”);
while(i<5)
{
printf(“a[%d]\t%5d\t%8u\n”,i,*(a+i),(a+i));
sum=sum+*(a + i++);
}
printf(“\n sum of array elements =%d”,sum);
getch();
}
WAP 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[]={1,2,3,4,5}, sumsq=0,sumc=0;
clrscr();
for(j=0;j<5;j++)
{
sumsq=sumsq+pow(*(j+b),2);
sumc=sumc+pow(b[j],3);
}
printf(“\n sum of squares of array elements :%d”,sumsq);
printf(“\n sum of cubes of array elements :%d”,sumc);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j=1,*p;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr();
printf(“\t elements of an array with their addresses\n\n”);
p=&a[0][0];
for(i=0;i<9;i++,j++)
{
printf(“%5d[%5u]”,*(p),p);
p++;
if(j==3)
{
printf(“\n”);
j=0;
}
}
getch();
}
Array of Pointers
#include<stdio.h>
#include<conio.h>
void main()
{
int *arrp[3];
Int arr1[3]={5,10,15},k;
clrscr();
for(k=0;k<3;k++)
{
arrp[k]=arr1+k;
printf(“\n\t address element\n”);
for(k=0;k<3;k++)
{
printf(“\t%u”,arrp[k]);
printf(“”\t%7d\n”,*(arrp[k]);
}
getch();
}
Pointers to pointers
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,*p,**q;
p=&a;
q=&p;
clrscr();
printf(\n value of a=%d address of a =%u”,a,&a);
printf(“\n through *p value of a=%d address of a=%u”,*p,p);
printf(“\n through **q value of a=%d address of a=%d”,**q,*q);
getche();
}
WAP to read string from keyboard and display it using character pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
char name[15],*ch;
clrscr();
printf(“\n enter your name :”);
gets(name);
ch=name;
while(*ch!=’\0’)
{
printf(“%c”,*ch);
ch++;
}
getche();
}
WAP to find length of a given string including and excluding spaces using pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20],*s;
int p=0,q=0;
clrscr();
printf(“\n enter string :”);
gets(str);
s=str;
while(*s!=’\0’)
{
printf(“%c”,*s);
p++;
s++;
if(*s==32)
q++;
}
printf(“\n length of string including spaces :%d”,p);
printf(“\n length of string excluding paces :%d”,p-q);
getche();
}
WAP to read two strings through the keyboard. Compare these two strings
character by character. Display the similar characters found in both the strings and
count the number of dissimilar characters.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20],l,*a,*b;
int c=0;
clrscr();
printf(“\n enter first string :”);
gets(str1);
printf(“\n enter second string :”);
gets(str2);
a=str1;
b=str2;
printf(“\n similar charaters found in both string :”);
while(*a!=’\0’)
{
if(stricmp(*a,*b)==0)
{
printf(“\n\t%c\t%c”,*a,*b);
l++;
}
else
c++;
a++;
b++;
}
if(c==0)
printf(“\n the string are identical “);
else
printf(“\n the strings are different at %d places”,c);
printf(“\n the string charaters are similar at %d paces.”,l);
getche();
}