We can easily add two numbers by using C program but in this article we are going to perform addition of two numbers using pointer.
Table of Contents
Pointers in C
Pointers in C are the variables that stores memory addresses as their values. Pointers point to the location of another variable in the memory, Enabling access and maipulation of data store in that memory address.
In C programming language we can decalre a pointer by using asterisk(*) symbol, indicating that it hold the memory address of a specific data type.
Adding Two Numbers Using Pointer
#include <stdio.h>
void addNumbers(int *n1, int *n2, int *sum)
{
*sum = *n1 + *n2;
}
void main()
{
int num1, num2, result;
printf("Enter the first number ");
scanf("%d", &num1);
printf("Enter the second number ");
scanf("%d", &num2);
addNumbers(&num1, &num2, &result);
printf("Sum of %d and %d = %d", num1, num2, result);
}
Output
In the above C program we perform addition of two numbers using pointers.
Happy Coding