Write a C program to illustrate the use of pointers in arithmetic operations.

 

Write a C program to illustrate the use of pointers in arithmetic operations.
Program:
#include<stdio.h>
main()
{
int a[10]={9,8,7,6,5,4,3,2,1};
int *pa=a;
char b[10]={'a','b','c','j','e','f','g','h','i','\0'};
char *pb=b;
float c[10]={9,8,7,6,5,4,3,2,1};
float *pc=c;
clrscr();
printf("\nInteger pointer arithmatic\n");
printf("*pa:%d\t",*pa);
printf("pa:%u\t",pa);
printf("pa++:%u\t",++pa);
printf("*pa:%d\t",*pa);
printf("pa:%u\t",pa);
printf("pa+5:%u\t",pa=pa+5);
printf("*pa:%d\t",*pa);
printf("pa:%u\t",pa);
printf("pa-a:%u\t",pa-a);
printf("pa:%u\t",pa);
printf("pa--:%u\t",--pa);
printf("*pa:%d\t",*pa);
printf("pa:%u\t",pa);
printf("pa-5:%u\t",pa=pa-5);
printf("*pa:%d\t",*pa);
printf("pa:%u\t",pa);
printf("\n\n");
printf("\nCharacter pointer arithmatic\n");
printf("*pb:%c\t",*pb);
printf("pb:%u\t",pb);
printf("pb++:%u\t",++pb);
printf("*pb:%c\t",*pb);
printf("pb:%u\t",pb);

printf("pb+5:%u\t",pb=pb+5);
printf("*pb:%c\t",*pb);
printf("pb:%u\t",pb);
printf("pb-b:%u\t",pb-b);
printf("pb:%u\t",pb);
printf("pb--:%u\t",--pb);
printf("*pb:%c\t",*pb);
printf("pb:%u\t",pb);
printf("pb-5:%u\t",pb=pb-5);
printf("*pb:%c\t",*pb);
printf("pb:%u\t",pb);
printf("\n\n");
printf("\nFloat pointer arithmatic\n");
printf("*pc:%f\t",*pc);
printf("pc:%u\t",pc);
printf("pc++:%u\t",++pc);
printf("*pc:%f\t",*pc);
printf("pc:%u\t",pc);
printf("pc+5:%u\t",pc=pc+5);
printf("*pb:%f\t",*pc);
printf("pc:%u\t",pc);
printf("pc-b:%u\t",pc-c);
printf("pc:%u\t",pc);
printf("pc--:%u\t",--pc);
printf("*pc:%f\t",*pc);
printf("pc:%u\t",pc);
printf("pc-5:%u\t",pc=pc-5);
printf("*pc:%f\t",*pc);
printf("pc:%u\t",pc);
getch();
}

 

 

Output:
Integer pointer arithmatic
*pa:9 pa:65426 pa++:65428 *pa:8 pa:65428 pa+5:65438
*pa:3 pa:65438 pa-a:6 pa:65438 pa--:65436 *pa:4 pa:65436
pa-5:65426 *pa:9 pa:65426

Character pointer arithmatic
*pb:a pb:65446 pb++:65447 *pb:b pb:65447 pb+5:65452
*pb:g pb:65452 pb-b:6 pb:65452 pb--:65451 *pb:f pb:65451
pb-5:65446 *pb:a pb:65446

Float pointer arithmatic
*pc:9.000000 pc:65456 pc++:65460 *pc:8.000000 pc:65460
pc+5:65480 *pb:3.000000 pc:65480 pc-b:6 pc:65480 pc--:654
76 *pc:4.000000 pc:65476 pc-5:65456 *pc:9.000000 pc:65456