C program to add two numbers using pointers
In this post, you will learn how to add two numbers using pointers in the C programming language. Such a type of basic program is useful to improve your logic building. There are different ways to add two numbers in C. Here, we have used the pointers.
A pointer is a variable whose value is the address of another variable, i.e., the direct address of the memory location. There are various types of pointers, such as null pointers, wild pointers, void pointers, and other types of pointers.
Addition of two numbers using pointers
In the given C program, we have two integer variables (x and y) and two pointer variables (p and q). The pointer variable p holds the address of x and pointer variable q holds the address of y and then assign the sum of x and y to the variable sum.
#include <stdio.h>
int main()
{
int x, y, *p, *q, sum;
printf("Enter two integers : \n");
scanf("%d%d", &x, &y);
p = &x;
q = &y;
sum = *p + *q;
printf("Sum of the numbers = %d\n", sum);
return 0;
}
Output of the above code:
Enter two integers :
342
422
Sum of the numbers = 764
Enter two integers :
920
933
Sum of the numbers = 1853
Related Articles
Convert double to int JavaAverage of two numbers in C
Swapping of two numbers in C using pointers
Armstrong number in C using function
Binary to decimal C program
Sum of array elements in C
Random number generator in C
Factorial program in c using while loop
Student mark sheet program in C
C program to sort names in alphabetical order
C program to find largest number in an array
Print first 10 natural numbers using while loop in C
Simple calculator program in C
C program for simple interest
Swap two numbers without using third variable in C
Radix sort program in C
Bit stuffing program in C
Bubble sort program in C
Decimal to hexadecimal in C
Hexadecimal to decimal in C
Quick sort program in C