In this program we discuss about how to add two pointer variables. Means here we perform some arithmetic operations like addition.
Take an Example Add 2 Pointers:
Input
-----
Input num1: 45
Input num2: 25
Output
-----
Sum = 70
Knowledge Required
Basic C programming and Pointers.
Logic Of Addition Of Two Pointers:
Learning programming languages from day one the basic mathematical problem to write in coding form that is addition of two numbers.
Here logic is very simple, first declare two variables of any type. Then similarly declares two pointer variables with same type as addresses are hold by pointers.
Secondly check the pointer variables are hold the addresses of variables which holds data and performs arithmetic operations on it.
Program Addition Of Two Operators:
** * C program to add two number using pointers */ #include <stdio.h> int main() { int num1, num2, sum; int *ptr1, *ptr2; printf("Enter any two numbers: "); scanf("%d%d", &num1, &num2); ptr1 = &num1; // ptr1 stores the address of num1 ptr2 = &num2; // ptr2 stores the address of num2 sum = *ptr1 + *ptr2; printf("Sum = %d", sum); return 0; }
The above code return output like this,
Output:
------
Enter any two numbers:
12
24
Sum = 36
Explanation:
Here *ptr1 and *ptr2 both pointer variables.
ptr1 = &num1 and ptr2 = &num2 holds the addresses of num1 and num2.
Then the line of code sum = *ptr1+*ptr2 addition of 2 numbers using pointers.
If you find any issue and interested to rewrite it then contact us.