Here, we have a basic program example to add two numbers using pointers in C and C++.
Code to add two numbers with pointers in C language
#include <stdio.h>
int main()
{
int num1, num2, *p, *q, sum;
printf("Enter first integer : \n");
scanf("%d", &num1);
printf("Enter second integer : \n");
scanf("%d", &num2);
p = &num1;
q = &num2;
sum = *p + *q;
printf("Sum of the numbers = %d\n", sum);
return 0;
}
Code to add two numbers with pointers in C++ language
#include<iostream>
using namespace std;
int main()
{
int num1, num2, *ptr1, *ptr2, sum=0;
cout<<"Enter First Number: ";
cin>>num1;
cout<<"Enter Second Number: ";
cin>>num2;
ptr1 = &num1;
ptr2 = &num2;
sum = *ptr1 + *ptr2;
cout<<"\nSum of Two Numbers = "<<sum;
cout<<endl;
return 0;
}