In this program, you'll learn to perform Arithmetic Operations using Pointers. The pointer is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or stores) an integer value, however an integer pointer holds the address of a integer variable. Adding or subtracting from a pointer moves it by a multiple of the size of the data type it points to. For example, assume we have a pointer to an array of 4-byte integers. Incrementing this pointer will increment its value by 4 (the size of the element).
#include<stdio.h>
#include<conio.h>
void main()
{
int no1, no2, *ptr1, *ptr2, result;
clrscr();
printf("Enter no1: ");
scanf("%d", &no1);
printf("Enter no2: ");
scanf("%d", &no2);
ptr1 = &no1;
ptr2 = &no2;
result = *ptr1 + *ptr2;
printf("\n Addition=%d", result);
result = *ptr1 - *ptr2;
printf("\n Subtraction=%d", result);
result = *ptr1 * *ptr2;
printf("\n Multiplication=%d", result);
result = *ptr1 / *ptr2;
printf("\n Division=%d",result);
getch();
}
Enter no1: 45
Enter no2: 2
Addition=47
Subtraction=43
Multiplication=90
Division=22